Basic question about creating and deleting structured variables

Just starting out? Need help? Post your questions and find answers here.
eNano
User
User
Posts: 13
Joined: Wed Jun 05, 2024 4:48 pm

Basic question about creating and deleting structured variables

Post 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?


User avatar
mk-soft
Always Here
Always Here
Posts: 6206
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Basic question about creating and deleting structured variables

Post by mk-soft »

Yes,
The contents will also be released
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Basic question about creating and deleting structured variables

Post 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.
eNano
User
User
Posts: 13
Joined: Wed Jun 05, 2024 4:48 pm

Re: Basic question about creating and deleting structured variables

Post by eNano »

Thanks a lot for the answers, it was driving me crazy
Post Reply