Clearing a Structure

Just starting out? Need help? Post your questions and find answers here.
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Clearing a Structure

Post 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
DE AA EB
Julian
Enthusiast
Enthusiast
Posts: 276
Joined: Tue May 24, 2011 1:36 pm

Re: Clearing a Structure

Post 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))
User avatar
STARGÅTE
Addict
Addict
Posts: 2235
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Clearing a Structure

Post by STARGÅTE »

Note, that you need an InitializeStructure() after a ClearStructure() to use items like lists and Arrays again.
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Clearing a Structure

Post by Demivec »

If reusing the structure, ResetStructure() can also be used instead of ClearStructure() + InitializeStructure().
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Clearing a Structure

Post 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.
DE AA EB
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Clearing a Structure

Post 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.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Clearing a Structure

Post 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.
sorry for my bad english
collectordave
Addict
Addict
Posts: 1310
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Clearing a Structure

Post by collectordave »

Yes just a note to say that structured variables are reset to their original state when using this would be good.
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply