Page 1 of 1

[SOLVED] Possible bug with Arrays and Pointer inside a Structure

Posted: Sat Oct 05, 2024 6:15 pm
by Mijikai
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:

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()

Re: Possible bug with Arrays and Pointer inside a Structure

Posted: Sat Oct 05, 2024 6:41 pm
by STARGÅTE
The definition of

Code: Select all

 *p.DAT[0]
means, you define an static array with 0 fields. When you then write to *d\p, this is not defined.

You probably want to do:

Code: Select all

Structure DAT
  v.l[0]
EndStructure

Structure DUMMY
  *p.DAT ; A pointer to DAT
  Array a.l(0,0)
  Array b.l(0,0)
EndStructure

Re: Possible bug with Arrays and Pointer inside a Structure

Posted: Sat Oct 05, 2024 6:58 pm
by Mijikai
@ STARGÅTE thanks for your reply, that explains it. :oops:
*p.DAT is indeed what i wanted to do :)

Re: [SOLVED] Possible bug with Arrays and Pointer inside a Structure

Posted: Sat Oct 05, 2024 10:52 pm
by idle
there should really be a description of this under static arrays in the structures section of the help
Call it a type iterator or something.