Hi,
How does ClearStructure() operates on nested and actual structure-members (not pointers to structures) ?
Thanks
ClearStructure on nested structures ?
Re: ClearStructure on nested 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:said wrote:How does ClearStructure() operates on nested and actual structure-members (not pointers to structures)?
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
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
Re: ClearStructure on nested structures ?
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
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!
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
Re: ClearStructure on nested structures ?
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:said wrote:Code: Select all
InitializeStructure(@x, Employee) ; <<<< comment this line -> crash lists colleague() and child() not initialized
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
Re: ClearStructure on nested structures ?
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
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
Re: ClearStructure on nested structures ?
The answer is yes.said wrote:Do CLearStructure() and InitializeStructure() operate on nested structures as well, yes or no ?
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.
Re: ClearStructure on nested structures ?
Thank you Demivec
Then why such a powerful feature is not mentioned in the doc
Then why such a powerful feature is not mentioned in the doc
Re: ClearStructure on nested structures ?
Yes, they are. They can be found in the help file under Reference Manual > Various Topics > Compiler Functions, and even online:said wrote:Then why such a powerful feature is not mentioned in the doc
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