Why does this code leads to data corruption (or stack corruption, not sure) and how it is possible to handle returning structure (not pointer) correctly in PB?
Code: Select all
; Create an empty space.
Define *space.cpSpace = cpSpaceNew() ; cpSpaceNew returns a pointer to structure stored inside external lib
; it is all OK, the *space data is valid
Define *testG.cpVect = cpSpaceGetGravity (*space) ; cpSpaceGetGravity returns a whole cpVect structure, not just a pointer
; now the first fields of *space data structure are corrupted [seems one first field which size is 4 bytes, as pointer size]
Code: Select all
typedef double cpFloat;
typedef struct cpVect{cpFloat x,y;} cpVect;
cpVect
cpSpaceGetGravity(const cpSpace *space)
{
return space->gravity;
}
Code: Select all
Macro cpFloat : d : EndMacro
Structure cpVect ; Align not needed, cause lib temporary re-compiled with 1 byte align
X.cpFloat
Y.cpFloat
EndStructure
cpSpaceGetGravity.i (*space.cpSpace) As "_cpSpaceGetGravity"; = cpVect