Pointer-pointer not valid after frame

Posted By: Joozey

Pointer-pointer not valid after frame - 01/26/09 20:25

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...
Posted By: jcl

Re: Pointer-pointer not valid after frame - 01/27/09 08:05

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.
Posted By: Joey

Re: Pointer-pointer not valid after frame - 01/27/09 14:05

so pointers become invalid after a wait call? that's interesting.
Posted By: FBL

Re: Pointer-pointer not valid after frame - 01/27/09 14:40

That's not interesting - that's horrible wink - but logical.
Posted By: jcl

Re: Pointer-pointer not valid after frame - 01/27/09 15:57

Pointers don't become invalid. Addresses become invalid.

int* mypointer is still valid after wait. But &mypointer isn't.
Posted By: Joozey

Re: Pointer-pointer not valid after frame - 01/28/09 15:22

That's great, it seems to work, and it does make sense smile.
Thanks alot for the answer.
Posted By: Joey

Re: Pointer-pointer not valid after frame - 01/28/09 16:54

since the stack should be restored, which register has changed that makes the addresses invalid? they're relative to what? the base pointer?
Posted By: EvilSOB

Re: Pointer-pointer not valid after frame - 01/28/09 18:48

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.
© 2024 lite-C Forums