Is this a safe way to clear an array?

BlitzMax Forums/BlitzMax Programming/Is this a safe way to clear an array?

Grey Alien(Posted 2007) [#1]
myarray = myarray[..0]

It works, so is it cool from a GC point of view?

If I rexpand the array with myarray = myarray[..1] any data I stored in slot [0] has indeed disappeared so it works e.g.

Strict

Local test$[]

test = test[..1]
test[0] = "hello"

test = test[..0]
test = test[..1]

Print test[0]



Vertex(Posted 2007) [#2]
test = Null?!


Grey Alien(Posted 2007) [#3]
Sorry let me be more specific. I want to clear it for REUSE not totally clear it forever.


FlameDuck(Posted 2007) [#4]
I want to clear it for REUSE not totally clear it forever.
The difference between what you're doing and what Vertex suggests is purely academic.


Vertex(Posted 2007) [#5]
Oh, ok. For byte, short, int, long, float and double it should work with MemClear(Array, Arraysize). For instances and strings too, but I dont't think, that GC is so clever, to detect this freeed resources.

cu olli


H&K(Posted 2007) [#6]
Sorry let me be more specific. I want to clear it for REUSE not totally clear it forever.
You still got the flu?


Grey Alien(Posted 2007) [#7]
I just wasn't sure if this would work:

Strict

Local test$[1]

test = Null

test = test[..1]
test[0] = "hi"

Print test[0]


but it does :-)

and yes I still have the flu a little bit but I'm not using it as an excuse ;-)


rdodson41(Posted 2007) [#8]
Just allocate a whole new array for the size you want:
Local test$[]

test = new String[2]
test[0] = "hello"
test[1] = "world"

Print test[0]
Print test[1]

test = new String[1]
test[0] = "hi"

Print test[0]



Grey Alien(Posted 2007) [#9]
Thanks rich, I know I can do that, but I'm specifically uses slices becuase it's part of a larger piece of code that dynamically grows the arrays.


bradford6(Posted 2007) [#10]
if you are going to reuse it, is it really necessary to clear it? maybe you could have a single element that designates 'clear' or 'full'

if arr[0]=1 then 'active' ( array is in use)
if arr[0] = 0 then 'clear' (don't use it)

or you could have another var.
arrayactive:int = true/false


rdodson41(Posted 2007) [#11]
I see Grey, then I guess what you were doing before is probably the best solution.