Page 1 of 1

Clear/erase arrays/structures

Posted: Sun Dec 22, 2024 4:10 am
by Thumper
prescript: I re-created this post here as this accidentally ended up in the Windows section, probably due to having many tabs open.

Before I ask, it has been fun as it seems that a few others are on the same path... recent questions have been relevant toward where I am at in my learning PureBasic. Actually, I have a new program well underway, my pre-compiler is already doing its job and things are progressing nicely.

Okay, the question here is what is the best way to "reset" an array and/or structure ?

For arrays, I'm just doing a for i = 1 to arraySize ( myArray ( ) ) and for structures, define a clearData.myStructure and overwriting my data myData = clearData.

Are there any concerns if I have arrays with structures in my structures ? Stuff like record(1)\filelist[1]\filename ?

I'll leave it that. The technique seems to work. Below is a simplified sample of what I've using just in case anyone spots anything I'm doing that is not advisable.

Code: Select all

     structure _playlistrecords
          filename.s
          description.s
          duration.i
     endStructure

     structure _record
          description.s
          playlistrecords.i
          playlist._playlistrecords [ 101 ]
     endStructure

     define s.s, i.i, j.i
     define dim record._record ( 500 )

     record(1)\description = "First record"
     record(1)\playlistrecords = 3
     record(1)\playlist[1]\filename = "First record, First item"
     record(1)\playlist[2]\filename = "First record, Second item"
     record(1)\playlist[3]\filename = "First record, Third item"

     record(2)\description = "Second record"
     record(2)\playlistrecords = 2
     record(2)\playlist[1]\filename = "Second record, First item"
     record(2)\playlist[2]\filename = "Second record, Second item"

     for i = 1 to 2
          s = s + str(i) + ":" + record(i)\description + chr(13) + chr(10)
          for j = 1 to record(i)\playlistrecords
               s = s + "   " + str(i) + "," + str(j) + ":" + record(i)\playlist[j]\filename + chr(13) + chr(10)
          next
          s = s + chr(13) + chr(10)
     next

     s = s + "----- CLEARING -----" + chr(13) + chr(10)

     define clearRecord._record

     for i = 0 to arraySize ( record ( ) )
          record(i) = clearRecord
     next

     for i = 1 to 2
          s = s + str(i) + ":" + record(i)\description + chr(13) + chr(10)
          for j = 1 to record(i)\playlistrecords
               s = s + "   " + str(i) + "," + str(j) + ":" + record(i)\playlist[j]\filename + chr(13) + chr(10)
          next
          s = s + chr(13) + chr(10)
     next

     messageRequester ( "TEST", s )

;    messageBox results captured using ^c
;
;    ---------------------------
;    TEST
;    ---------------------------
;    1:First record
;       1,1:First record, First item
;       1,2:First record, Second item
;       1,3:First record, Third item
;
;    2:Second record
;      2,1:Second record, First item
;      2,2:Second record, Second item
;
;    ----- CLEARING -----
;    1:
;
;    2:
;
;
;    ---------------------------
;    OK
;    ---------------------------

Re: Clear/erase arrays/structures

Posted: Sun Dec 22, 2024 4:30 am
by idle
Doesn't FreeArray() work for you?

Re: Clear/erase arrays/structures

Posted: Sun Dec 22, 2024 10:03 pm
by STARGÅTE
ResetStructure() - clears a structured memory area and initialize it to be ready to use.

Re: Clear/erase arrays/structures

Posted: Sun Dec 22, 2024 10:28 pm
by Thumper
[I have had to type this several times... the forum asks me to re-login, even after only a few minutes...]

Okay, I had not considered that as something about it does not seem quite right. Perhaps some historical limitation lingers in my mind.

Being that I need to dim the array first for the compiler, I can follow it up with a procedure to dim it again.

Code: Select all

     global dim record._record ( 500 )

     procedure reset_record ( )
          protected.i resetRecords = arraySize ( record ( ) )
          freeArray ( record ( ) )
          global dim record._record ( resetRecords )
     endProcedure
I cannot see a cleaner way to do this but it will only be for 5 different global arrays.

For non-globals, this will not be needed very often and I'll just have to hand-write the arraySize, freeArray and dim.

Meanwhile, I'm enjoying the capabilities of the structures where they can be nested with variable length strings and arrays.

Cheers

Re: Clear/erase arrays/structures

Posted: Mon Dec 23, 2024 5:57 am
by Thumper
STARGÅTE wrote: Sun Dec 22, 2024 10:03 pm ResetStructure() - clears a structured memory area and initialize it to be ready to use.
It was to find that one! Your link wasn't working (could be my ISP) but it gave me the keyword "compiler functions".

ResetStructure looks good for the non-array variables. Arrays looks to be better wiped out by the free and (re) dim.

Thanks for the inputs.

Re: Clear/erase arrays/structures

Posted: Mon Dec 23, 2024 4:03 pm
by robertfern
If by reset, you mean set all values back to zero, you can use FillMemory like so...
mylen.l = 100
Dim MyArray.l(mylen)
...
; use array
...
; reset array
FillMemory(@ MyArray(), mylen -1, 0, #PB_Long)

Re: Clear/erase arrays/structures

Posted: Tue Dec 24, 2024 1:00 am
by mk-soft
robertfern wrote: Mon Dec 23, 2024 4:03 pm If by reset, you mean set all values back to zero, you can use FillMemory like so...
mylen.l = 100
Dim MyArray.l(mylen)
...
; use array
...
; reset array
FillMemory(@ MyArray(), mylen -1, 0, #PB_Long)
No,
that's not the right way. If strings or array, list map are present, this inevitably leads to memory leaks.