Page 1 of 1

Basic question about creating and deleting structured variables

Posted: Wed May 07, 2025 9:05 pm
by eNano
Hi,
I have a conceptual doubt about defining and deleting structured data with complex fields inside
If I define an array of structured data with arrays and maps and list inside, and then I delete that element, are all the elements inside of that structure deleted as well ? or I need to delete them one by one before I delete the main structure?
Here is a code to show my doubt:

Code: Select all

Structure fieldStucture
  field1.i
  field2.i
EndStructure
Structure sampleStructure
  ID.i
  Array arrayField.i(0)
  List listField.i()
  Map MapField.i()
  List strListField.fieldStucture()
EndStructure
Global NewList strucList.sampleStructure()

For n = 0 To 10
  AddElement( strucList() )
  strucList()\ID = n
  ReDim strucList()\arrayField(10)
  AddElement( strucList()\listField() )
  AddMapElement( strucList()\MapField(), "1" )
  AddElement( strucList()\strListField() )
  strucList()\strListField()\field1 = n
  strucList()\strListField()\field2 = n+1
Next

SelectElement( strucList(), 4 )


DeleteElement(strucList())
;Are all the elements of arrayField, listField, MapField, and strListField deleted too?



Re: Basic question about creating and deleting structured variables

Posted: Wed May 07, 2025 10:16 pm
by mk-soft
Yes,
The contents will also be released

Re: Basic question about creating and deleting structured variables

Posted: Wed May 07, 2025 10:20 pm
by Demivec
All elements and sub-elements are deleted.

The special case that does require you to take special steps is if elements or sub-elements contain pointers to allocated memory or other elements. Those would require you to free those memory areas first if those memory pointers were the only place the memory addresses were kept. However if the structure contains pointers to variables that were defined normally (in a declaration of some sort) you wouldn't have to worry about it. PureBasic frees a variable's memory when it goes out of scope or at the latest, when the program ends.

Re: Basic question about creating and deleting structured variables

Posted: Thu May 08, 2025 5:22 pm
by eNano
Thanks a lot for the answers, it was driving me crazy