Gamestudio Links
Zorro Links
Newest Posts
Help with plotting multiple ZigZag
by degenerate_762. 04/30/24 23:23
M1 Oversampling
by 11honza11. 04/30/24 08:16
Trading Journey
by howardR. 04/28/24 09:55
Zorro Trader GPT
by TipmyPip. 04/27/24 13:50
Data from CSV not parsed correctly
by jcl. 04/26/24 11:18
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
1 registered members (AndrewAMD), 938 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
firatv, wandaluciaia, Mega_Rod, EternallyCurious, howardR
19050 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Pointer-pointer not valid after frame #248428
01/26/09 20:25
01/26/09 20:25
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline OP
Expert
Joozey  Offline OP
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
Code:
typedef struct {
  int value;
} PTR;
 
 
void processPointer( PTR **p ) {
 
  wait(1); //the wait causes **p to be invalid
 
  *p = (PTR*)malloc(sizeof( PTR ));
  (*p)->value = 1;
}
 
 
void main() {
  PTR* p = NULL;
  processPointer( &p );
 
  while( !p ) {
  	draw_text("Still in while loop...", 10, 10, vector(255, 255, 50));
  	wait(1);
  }
 
  error("check!");
}


As stated, the wait(1); in processPointer() makes the **p pointer become invalid somehow. At least, the error("check!"); is never called when the processPointer() function waits one frame. If I comment out the wait, the "check!" error pops up as expected.

I can't see any reason why this would happen further than that waiting a frame screws up...


Click and join the 3dgs irc community!
Room: #3dgs
Re: Pointer-pointer not valid after frame [Re: Joozey] #248495
01/27/09 08:05
01/27/09 08:05
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
This is a good question for our Gamestudio quiz: Why can't this code work?

A hint for finding out yourself - run this program:

void main()
{
int i = 0;
long ptr1 = &i;
wait(1);
long ptr2 = &i;
printf("%d %d",ptr1,ptr2);
}

You're passing the address of a local variable to your function. But in C/C++, local variables have no fixed adresses. They are stored on the stack. That is a temporary area in the computer memory that's shared by all functions. While local variable content is preserved by wait(), they sit in a different place on the stack every frame, and have different addresses.

If you want a variable to be on an absolute address and not on the stack, use the 'static' modifier. You can read more about 'static' in a C/C++ book or tutorial.

Re: Pointer-pointer not valid after frame [Re: jcl] #248530
01/27/09 14:05
01/27/09 14:05
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
so pointers become invalid after a wait call? that's interesting.

Re: Pointer-pointer not valid after frame [Re: Joey] #248537
01/27/09 14:40
01/27/09 14:40
Joined: Sep 2003
Posts: 9,859
F
FBL Offline
Senior Expert
FBL  Offline
Senior Expert
F

Joined: Sep 2003
Posts: 9,859
That's not interesting - that's horrible wink - but logical.

Re: Pointer-pointer not valid after frame [Re: FBL] #248545
01/27/09 15:57
01/27/09 15:57
Joined: Jul 2000
Posts: 27,986
Frankfurt
jcl Offline

Chief Engineer
jcl  Offline

Chief Engineer

Joined: Jul 2000
Posts: 27,986
Frankfurt
Pointers don't become invalid. Addresses become invalid.

int* mypointer is still valid after wait. But &mypointer isn't.

Re: Pointer-pointer not valid after frame [Re: jcl] #248684
01/28/09 15:22
01/28/09 15:22
Joined: Oct 2004
Posts: 4,134
Netherlands
Joozey Offline OP
Expert
Joozey  Offline OP
Expert

Joined: Oct 2004
Posts: 4,134
Netherlands
That's great, it seems to work, and it does make sense smile.
Thanks alot for the answer.


Click and join the 3dgs irc community!
Room: #3dgs
Re: Pointer-pointer not valid after frame [Re: Joozey] #248692
01/28/09 16:54
01/28/09 16:54
Joined: Jan 2003
Posts: 4,615
Cambridge
Joey Offline
Expert
Joey  Offline
Expert

Joined: Jan 2003
Posts: 4,615
Cambridge
since the stack should be restored, which register has changed that makes the addresses invalid? they're relative to what? the base pointer?

Re: Pointer-pointer not valid after frame [Re: Joey] #248709
01/28/09 18:48
01/28/09 18:48
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
The address becomes invalid because when the stack restores all the stacked
variables, they dont necessarily end up in the same position in memory as they
were before, which means their address has changed.
Therefore, addresses become invalid, but pointers are fine because they are pointing
to another variable, not its actual address.

That how I understand it anyway.


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial

Moderated by  old_bill, Tobias 

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