- Joined
- Feb 2, 2012
- Messages
- 1,667
- Reaction score
- 115
- Points
- 78
Hi guys,
I'm encountering a curious error while using pointers... I couldn't understand what was going on so I started to log here and there, and make experiments... and here is the point i manage to get to:
I have a function to which a ThrusterHandle is passed.
in my class I have a structure called psg which is an array that contains a thrusterhandle pointer variable, and I store the address of the thruster handle in my structure, line 2 inside the function psg[nPsg].th=&th (nPsg is just a counter).
Now, to access the original thruster handle I'm supposed to use *psg[nPsg].th right? and that seems to work at the beginning.
I added the last part of code to log some lines with a test function for the thruster which will show me if the pointer works or not. Well if I leave it as it is in the code it works and i get the log of everything but...
If I change this:
with simply this:
bang... I get a CTD. The log shows that the cycle happens for the first two times so I see the thdir o psg[0] components printed twice before CTD... now, why this does not happen if I leave the first line of code? it seems that the psg[0].th pointer becomes invalid at the third loop, but I really don't get this... Any help?
Thanks in advance!
:tiphat:
I'm encountering a curious error while using pointers... I couldn't understand what was going on so I started to log here and there, and make experiments... and here is the point i manage to get to:
I have a function to which a ThrusterHandle is passed.
Code:
PSTREAM_HANDLE Multistage2015::AddExhaustStreamGrowing(THRUSTER_HANDLE th, const VECTOR3 & pos, PARTICLESTREAMSPEC * pss = 0 , bool growing=FALSE, double growfactor_size=0, double growfactor_rate=0, bool count=TRUE)
{
psg[nPsg].pss=*pss;
psg[nPsg].th=&th;
psg[nPsg].psh[0]=AddExhaustStream(*psg[nPsg].th,pos,pss);
if(count){ nPsg++;}
///Log to understand what's happening...
VECTOR3 thdir;
GetThrusterDir(*psg[nPsg].th,thdir);
sprintf(logbuff,"nPsg:%i %.3f %.3f %.3f",nPsg,thdir.x,thdir.y,thdir.z);
oapiWriteLog(logbuff);
return psg[nPsg-1].psh[0];
}
Now, to access the original thruster handle I'm supposed to use *psg[nPsg].th right? and that seems to work at the beginning.
I added the last part of code to log some lines with a test function for the thruster which will show me if the pointer works or not. Well if I leave it as it is in the code it works and i get the log of everything but...
If I change this:
Code:
GetThrusterDir(*psg[nPsg].th,thdir);
Code:
GetThrusterDir(*psg[0].th,thdir);
bang... I get a CTD. The log shows that the cycle happens for the first two times so I see the thdir o psg[0] components printed twice before CTD... now, why this does not happen if I leave the first line of code? it seems that the psg[0].th pointer becomes invalid at the third loop, but I really don't get this... Any help?
Thanks in advance!
:tiphat: