Page 1 of 1

Clearing a Structure

Posted: Sat Dec 24, 2016 5:10 pm
by davido
Below I list a simplified version of some code in one of my programs.

I need to clear the structure.
Currently I clear it by looping through the structures.
I would like to use something like ClearStructure but the manual seems to imply that ClearStructure can only be used with pointers.
I'm not clear, as it were, how best to proceed.

Could someone please advise me of the most efficient way of clearing the structure?

Code: Select all

 EnableExplicit


Structure BookList
  List BookNames$()
EndStructure
Structure SeriesList
  List   Series.BookList()
EndStructure
Structure Details
  List Authors.SeriesList()
EndStructure

Global Library.Details

Re: Clearing a Structure

Posted: Sat Dec 24, 2016 5:24 pm
by Julian

Code: Select all

 EnableExplicit


Structure BookList
  List BookNames$()
EndStructure
Structure SeriesList
  List   Series.BookList()
EndStructure
Structure Details
  List Authors.SeriesList()
EndStructure

Global Library.Details

ClearStructure(@Library, Details)

ShowMemoryViewer(@Library, SizeOf(Details))

Re: Clearing a Structure

Posted: Sat Dec 24, 2016 6:59 pm
by STARGÅTE
Note, that you need an InitializeStructure() after a ClearStructure() to use items like lists and Arrays again.

Re: Clearing a Structure

Posted: Sat Dec 24, 2016 7:41 pm
by Demivec
If reusing the structure, ResetStructure() can also be used instead of ClearStructure() + InitializeStructure().

Re: Clearing a Structure

Posted: Sat Dec 24, 2016 11:52 pm
by davido
@Julian,
Thank you very much for pointing me in the correct direction. :D

@STARGÅTE & Demivec,
Thank you, both, for the additional information.

Re: Clearing a Structure

Posted: Sat Apr 11, 2020 3:10 pm
by collectordave
Hi all

Just wanted to check.

I have a structure and declare a variable with that structure.

Maps are reset but still usable but there are no map elements.

Arrays appear to be reset to the original number of elements declared when the structure was declared not to the number of elements currently declared is this correct.

Code: Select all

  Structure Person
    Map Friends.s()
    Array nums.i(2)
  EndStructure
  
  Define Henry.Person
  
  
  Henry\Friends("1") = "Paul"
  Henry\Friends("2") = "Mary"
  Henry\nums(2) = 234
  
  ReDim Henry\nums(11)
  
   Henry\nums(9) = 468
  
  Debug Henry\Friends("1") 
  Debug Henry\Friends("2") 
  Debug Henry\nums(2)
  Debug Henry\nums(9)
  ResetStructure(@Henry, Person)
  
  ; Will print an empty string as the whole structure has been reseted. The map is still usable but empty.
  ;
  Debug Henry\Friends("1")
  Debug Henry\nums(2)
  
    Henry.Person\Friends("1") = "Peter"
   Henry\nums(9) = 468
   Debug Henry\Friends("1")   
   Debug Henry\Friends("2") 
   Debug Henry\nums(9)


I get a memory access error the line ' Henry\nums(9) = 468' second time around.

Re: Clearing a Structure

Posted: Sat Apr 11, 2020 4:03 pm
by Josh
collectordave wrote:Arrays appear to be reset to the original number of elements declared when the structure was declared not to the number of elements currently declared is this correct.
This is purely a matter of definition. I think it is ok, especially in connection with the fact that lists are also reset to their original size.

However, a note in the help file would not be bad.

Re: Clearing a Structure

Posted: Sun Apr 12, 2020 5:32 am
by collectordave
Yes just a note to say that structured variables are reset to their original state when using this would be good.