Is this a safe way to clear an array?

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

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]


test = Null?!

Sorry let me be more specific. I want to clear it for REUSE not totally clear it forever.

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.

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

Sorry let me be more specific. I want to clear it for REUSE not totally clear it forever.
You still got the flu?

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 ;-)

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]


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.

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

I see Grey, then I guess what you were doing before is probably the best solution.