Gamestudio Links
Zorro Links
Newest Posts
Free Live Data for Zorro with Paper Trading?
by dr_panther. 05/18/24 11:01
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (7th_zorro, dr_panther), 724 guests, and 3 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Trouble accessing/assigning struct elements #168087
11/18/07 13:47
11/18/07 13:47
Joined: Oct 2006
Posts: 36
BigM Offline OP
Newbie
BigM  Offline OP
Newbie

Joined: Oct 2006
Posts: 36
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!

Re: Trouble accessing/assigning struct elements [Re: BigM] #168088
11/18/07 15:59
11/18/07 15:59
Joined: Sep 2007
Posts: 14
X
xykynan Offline
Newbie
xykynan  Offline
Newbie
X

Joined: Sep 2007
Posts: 14
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.

Re: Trouble accessing/assigning struct elements [Re: xykynan] #168089
11/18/07 17:55
11/18/07 17:55
Joined: Oct 2006
Posts: 36
BigM Offline OP
Newbie
BigM  Offline OP
Newbie

Joined: Oct 2006
Posts: 36
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!

Re: Trouble accessing/assigning struct elements [Re: BigM] #168090
11/18/07 23:35
11/18/07 23:35
Joined: Oct 2006
Posts: 36
BigM Offline OP
Newbie
BigM  Offline OP
Newbie

Joined: Oct 2006
Posts: 36
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


Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1