ClearStructure on nested structures ?

Just starting out? Need help? Post your questions and find answers here.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

ClearStructure on nested structures ?

Post by said »

Hi,

How does ClearStructure() operates on nested and actual structure-members (not pointers to structures) ?

Thanks
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ClearStructure on nested structures ?

Post by TI-994A »

said wrote:How does ClearStructure() operates on nested and actual structure-members (not pointers to structures)?
Hi said. The ClearStructure() function essentially zeroes out all values stored in an initialised structure without destroying the structure definition or freeing the initialised structure. This example may better illustrate this:

Code: Select all

Structure Person
  name.s
  age.i
EndStructure

Structure Employee
  personnel.Person
  ID.i
EndStructure

Define x.Employee
With x
  \personnel\name = "Said"
  \personnel\age = 30
  \ID = 101
EndWith

Debug ">> x defined as structure, Employee"

With x
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug ""    
EndWith

ClearStructure(@x, Employee)
Debug ">> contents of structure x cleared"

With x
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug ""
EndWith

With x
  \personnel\name = "Mr Said"
  \personnel\age = 40
  \ID = 999
EndWith  

Debug ">> structure x still available and re-populated"

With x
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug ""    
EndWith

Define y.Employee
With y
  \personnel\name = "TI-994A"
  \personnel\age = 45
  \ID = 123
EndWith

Debug ">> y defined as structure, Employee - still available"

With y
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug ""    
EndWith
Hope it answers your question. :)
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: ClearStructure on nested structures ?

Post by said »

Hi TI-994A,

Thanks for your reply, so to clarify ClearStructure() operates in recursive manner on nested structures. It looks like the fields of the nested structures are cleared as if they were fields of the 'father' structure, i have re-worked your example adding 2 lists ... the same InitializeStructure() is enough for both lists to get initialized

Code: Select all

Structure Person
  name.s
  age.i
  List child.s()
EndStructure

Structure Employee
  personnel.Person
  ID.i
  List colleague.s()
EndStructure

Define x.Employee
With x
  \personnel\name = "Said"
  \personnel\age = 30
  \ID = 101
  AddElement( \personnel\child()) : \personnel\child() = "said-1"
  AddElement( \colleague()) : \colleague() = "TI-994A"
EndWith

Debug ">> x defined as structure, Employee"

With x
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug "Child #: " + \personnel\child()
  Debug ""   
EndWith

ClearStructure(@x, Employee)
InitializeStructure(@x, Employee)             ; <<<< comment this line -> crash lists colleague() and child() not initialized

Debug ">> contents of structure x cleared"

With x
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug "Colleague #: " + Str(ListSize(\colleague()))
  Debug "Child #: " + Str(ListSize(\personnel\child()))
  Debug ""
EndWith

With x
  \personnel\name = "Mr Said"
  \personnel\age = 40
  \ID = 999
  AddElement( \colleague()) : \colleague() = "Mr TI-994A"           ; relying only on InitializeStructure(@x, Employee), the list is initialized
  AddElement( \personnel\child()) : \personnel\child() = "said-2"   ; relying only on InitializeStructure(@x, Employee), the list is initialized
EndWith 

Debug ">> structure x still available and re-populated"

With x
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug "Colleague #: " + Str(ListSize(\colleague()))
  Debug "Child #: " + Str(ListSize(\personnel\child()))
  Debug ""   
EndWith

Define y.Employee
With y
  \personnel\name = "TI-994A"
  \personnel\age = 45
  \ID = 123
EndWith

Debug ">> y defined as structure, Employee - still available"

With y
  Debug "Name: " + \personnel\name
  Debug "Age: " + Str(\personnel\age)
  Debug "ID #: " + Str(\ID)
  Debug ""   
EndWith
Now an important question, does the code above yield a memory leak ? Ot it is safe ? The lists items prior to ClearStructure() were freed properly ? I have some doubts!
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ClearStructure on nested structures ?

Post by TI-994A »

said wrote:

Code: Select all

InitializeStructure(@x, Employee)   ; <<<< comment this line -> crash lists colleague() and child() not initialized
Hello again said. When a variable structure is defined, any array, list or map within it is automatically initialised. The ClearStructure() function zeroes out all native variables, and frees all arrays, lists and maps within the structure. These have to be re-initialised, either with the InitializeStructure() function, or individually, with the Dim, NewList, or NewMap directives:

Code: Select all

Structure Person
  name.s
  age.i
  List child.s()
EndStructure

Define x.Person
With x
  \name = "Said"
  \age = 30
  AddElement(\child()) : \child() = "said-1"
EndWith

Debug ">> x defined as structure, Person"
With x
  Debug "Name: " + \name
  Debug "Age: " + Str(\age)
  Debug "Child: " + \child()
  Debug ""  
EndWith

Debug ">> x cleared and only List re-initialised"
ClearStructure(x, Person)
;InitializeStructure(x, Person)
NewList x\child()   ;List re-initialised directly

With x
  \name = "TI-994A"
  \age = 45
  AddElement(\child()) : \child() = "TI-Basic"
EndWith

With x
  Debug "Name: " + \name
  Debug "Age: " + Str(\age)
  Debug "Child: " + \child()
  Debug ""  
EndWith
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: ClearStructure on nested structures ?

Post by said »

Hi TI-994A,

The point i am trying to figure out here is a clear answer to this question:

Do CLearStructure() and InitializeStructure() operate on nested structures as well, yes or no ?

Looking at the example, they seem to do so (in the example above one call to InitializeStructure(@x, Employee) was initializing not only the member-field list Colleauge() which is expected as per the doc, but the the list Child() that is present in the nested structure member Person as well

I hope i made myself clear and thanks for your time

Said
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: ClearStructure on nested structures ?

Post by Demivec »

said wrote:Do CLearStructure() and InitializeStructure() operate on nested structures as well, yes or no ?
The answer is yes.

It should be noted that there are some things to be aware of when pointers are part of a structure but they mainly involve being aware of properly handling the memory the pointers reference yourself or manually. They won't be handled as a nested structure and will be treated as the end of the line as far as the automatic handling of nesting goes. You would have to deal with what they point to by separately initializing or clearing them, as well as their nested structures for each pointer involved that does not point to an already existing structure.
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: ClearStructure on nested structures ?

Post by said »

Thank you Demivec

Then why such a powerful feature is not mentioned in the doc :?:
User avatar
TI-994A
Addict
Addict
Posts: 2512
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: ClearStructure on nested structures ?

Post by TI-994A »

said wrote:Then why such a powerful feature is not mentioned in the doc :?:
Yes, they are. They can be found in the help file under Reference Manual > Various Topics > Compiler Functions, and even online:

PureBasic Online Manual - Compiler Functions
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply