(no subject)

Mar. 24th, 2009 01:11 pm
symbioidlj: (Default)
[personal profile] symbioidlj
I think I figured out my pointer problem.

I was doing something like (pseudocode here):

int* bullet.x;
int* ship.x;

bullet.x = ship.x;

What this is doing is assigning the memory address of ship.x to bullet.x...

So when I change ship.x so does bullet.x -- this is not what we want.

We want to initialize the value of bullet.x to start at the same spot the ship is, but then when the ship moves, bullet.x should not change with the ship. Because it is now on its own trajectory.

So to copy the value, and not point to the memory occupied by the ship.x, we need to dereference (god, this is why it sucks to go back to programming after a hiatus, all things you should know easily forgotten, especially with pointers):

bullet.x = *(ship.x)

This dereferences the memory address, and tells it actually assign the VALUE not the ADDRESS of ship.x. Now, if ship.x changes, the value itself stays, since bullet.x now has it's own copy of the data, and is not just pointing to ship.x

At least, a cursory glance at C shit seems to indicate this is how it works. I forgot it all since it's been like half a year since I last touched code like this in any detail.

Anybody smarter at programming that can confirm if this is right???

Date: 2009-03-24 08:30 pm (UTC)
From: [identity profile] georgedorn.livejournal.com
Yup, that's basically right, and yup, managing pointers sucks.

One big problem is that if those aren't actually pointers to ints but pointers to a data structure, and that data structure contains more pointers, dereferencing the top-level pointer won't dereference the lower-level items. To do that, you need to implement a deep copy function yourself.

Date: 2009-03-25 12:10 am (UTC)
From: [identity profile] symbioid.livejournal.com
well I suppose I used poor notation.

bullet->x = *(ship->x)

Yet, I'm getting an error :(

(invalid type argument of `unary *') Hmm...

I'll keep plugging away. Fuckin' pointers. Thanks for the reply, though.

Date: 2009-03-25 12:16 am (UTC)
From: [identity profile] georgedorn.livejournal.com
That's because:

foo->bar

Is actually shorthand for:

*(foo.bar)

Date: 2009-03-25 04:48 am (UTC)
From: [identity profile] symbioid.livejournal.com
Have you seen this?

http://www.storytron.com/index.php

Apparently it's a project from Chris Crawford.

It's some sort of programming language/toolkit designed for creating stories/games.

I figured you've been on an IF kick recently, and with your love of programming, maybe this might interest you.

Saw it on boingboing.

Profile

symbioidlj: (Default)
symbioidlj

November 2015

S M T W T F S
1 234567
891011121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 21st, 2025 05:01 am
Powered by Dreamwidth Studios