pointer in a function containig wait

Posted By: alibaba

pointer in a function containig wait - 04/07/18 12:38

It's been some time since I've used 3DGS so it might be a stupid question.
(Code may contain errors since I've just typed from the top of my head)
Code:
void foo(int* i)
{
*i=5;
wait(1);
*i=6;
}

void main()
{
int a=0;
foo(&a);
wait_for(foo);
printf("%d", a);
}



This example gives me a "5" instead of a "6". Are pointers lost when using waits in a function? Is there a workaround?
Posted By: Superku

Re: pointer in a function containig wait - 04/07/18 13:35

The problem is that wait_for sends the main() function into a wait state as well, of which you are aware, and then after wait_for a new variable with the same name and value is "allocated" (check their pointers with printf).
The solution or workaround rather is to better not use wait whenever you can in my opinion (or make it a global variable/ object if you have to).
Posted By: Kartoffel

Re: pointer in a function containig wait - 04/07/18 17:09

another workaround would be using dynamic memory allocation. still, like Superku said, avoiding wait as much as possible is probably the best way.
Posted By: alibaba

Re: pointer in a function containig wait - 04/08/18 14:47

Thanks for your answers. I think I'll go the global variable way. I need wait, because it's a heavy instruction, which needs some seconds and I don't want the game to freeze during that time.
Posted By: txesmi

Re: pointer in a function containig wait - 04/08/18 15:10

You might declare that local variable as static variable too.
Posted By: alibaba

Re: pointer in a function containig wait - 04/08/18 15:28

AH! How could I've missed that!? Thank you! It totally makes sense when reading Superkus and your answer.
© 2024 lite-C Forums