Trouble accessing/assigning struct elements

Posted By: BigM

Trouble accessing/assigning struct elements - 11/18/07 13:47

Hi,

I have an array of 2 pointers to a custom struct. In one of the arrays I keep a set of game values generated each frame whereas the other has the same variables but from the previous frame, so, each frame I swap both pointers and overwrite the oldest of both structs:

typedef struct{
dFloat s;
dFloat a;
dFloat lck[2];
} Stuff;

Stuff* M[2];
Stuff* Mplaceholder = NULL;

Mplaceholder = M[0];
M[0] = M[1];
M[1] = Mplaceholder;

So far so good. The problem is, when I try something like
(M[1]).s=1;

and then output the .s and .a contents with

draw_text(str_for_num(0, (M[1]).s), 40, 20, vector(255,255,255));
draw_text(str_for_num(0, (M[1]).a), 40, 40, vector(255,255,255));

they are BOTH set to 1. If I set .s to 1 and .a to 2, then they will both become 2!!! Basically, both .s and .a get set to the last value assigned to any of them.

I tried using '->' instead of '.', no parentheses, '*' before array name but these solutions all led to crashes. I am defining the struct variables at a global level, so I assume everything is set to 0. I also commented out the initial pointer swap step, but to no avail...

The manual is sparse regarding the use of struct pointer arrays. So, thanks for any help!
Posted By: xykynan

Re: Trouble accessing/assigning struct elements - 11/18/07 15:59

I'm relatively new to programing, but I think when you set two pointers equal, they point to the same memory location, and, when you change a value using a pointer, it changes the data at that location.
What you need is two seperate memory locations and then you need to copy each parameter into the other location.
Sorry if that didn't make any sense
Here's what I would do:

copy_stuff (Stuff* a, Stuff* b)
{
a->s = b->s;
a->a = b->a;
a->lck[0] = b->lck[0];
a->lck[1] = b->lck[1];
}

Stuff* M[2];
M[0] = malloc(sizeof(Stuff));
M[1] = malloc(sizeof(Stuff));
Stuff* Mplaceholder = NULL;

copy_stuff (Mplaceholder, M[0]);
copy_stuff (M[0], M[1]);
copy_stuff (M[1], Mplaceholder);

Hope this helps.
Posted By: BigM

Re: Trouble accessing/assigning struct elements - 11/18/07 17:55

Hummm... I see what you mean, but do bear in mind that I swapped the pointers precisely to avoid the overhead of such a copy_stuff function.

However, I think I made a noob boo boo... I didn't know I had to explicitly allocate the memory for a pointer

This post points it out, just like you did. I will try the code and post back.

Thanks!
Posted By: BigM

Re: Trouble accessing/assigning struct elements - 11/18/07 23:35

Using malloc to allocate the memory for each of the structs in the M array didn't make any difference: assigning a value to .a seems to override the value assigned to .s, independently of performing any pointer swapping before.

I gave up and am now using straight, non pointer, struct arrays. I am copying them with memcpy, as per this post, which is definitely easier than copying variable by variable (why doesn't Struct1 = Struct2 work? Structure copy has been around in ANSI C for a very long while www.thescripts.com/forum/thread218971.html).

I still don't understand what was wrong with my implementation, and the bug seemed to be independent of the pointer swap. If anyone can shed some light on the issue it would be great: I still think that swapping pointers is more efficient than copying whole structures.

Thanks
© 2024 lite-C Forums