[SOLVED] Possible bug with Arrays and Pointer inside a Structure
Posted: Sat Oct 05, 2024 6:15 pm
If a structure is arranged like this, the pointer might corrupt arrays or lead to crashes!
Maybe there is a bug within the memory management of arrays?
Example:
Maybe there is a bug within the memory management of arrays?
Example:
Code: Select all
;PureBasic 6.12 LTS (x64)
;Linux (Debian 12)
EnableExplicit
Structure DAT
v.l
EndStructure
Structure DUMMY
*p.DAT[0];<- a pointer to something - should not influence the arrays (if .DAT[0] is removed it works fine)!
Array a.l(0,0)
Array b.l(0,0)
EndStructure
Procedure.i Main()
Protected *d.DUMMY
*d = AllocateStructure(DUMMY)
*d\p = *d\a()
Dim *d\a(10,10)
Dim *d\b(10,10)
*d\a(0,0) = 1
*d\b(0,0) = 1
Debug "ArraySize a: " + Str(ArraySize(*d\a(),1)) + " & " + Str(ArraySize(*d\a(),2))
Debug "ArraySize b: " + Str(ArraySize(*d\b(),1)) + " & " + Str(ArraySize(*d\b(),2))
*d\p = *d\b();<- if *p contains a pointer to the second array the first one will be corrupted!?
Dim *d\b(12,12)
*d\b(0,0) = 1
Debug "ArraySize a: " + Str(ArraySize(*d\a(),1)) + " & " + Str(ArraySize(*d\a(),2));<- array corrupted!?
Debug "ArraySize b: " + Str(ArraySize(*d\b(),1)) + " & " + Str(ArraySize(*d\b(),2))
FreeStructure(*d)
ProcedureReturn #Null
EndProcedure
End Main()