GLBasic forum

Main forum => GLBasic - en => Topic started by: Asmodean on 2012-Aug-28

Title: Dimdel problem
Post by: Asmodean on 2012-Aug-28
hi all,

I've a little problem with dimdel. I got the Error: 
bounds: [ 100]
access: [ 100]
Ausserhalb des DIM Bereichs (Array out of bounds).

But the Array ist not out of bounds.

Code (glbasic) Select

GLOBAL Array[]
DIM Array[200]

FOR i = 0 TO 199
Array[i]=i
NEXT

FOR i = 0 TO 199
DIMDEL Array[],i
NEXT


After I double the Array-lengh to 400 it works.

Can someone help what I'm doing wrong?
Title: Re: Dimdel problem
Post by: MrTAToad on 2012-Aug-28
What happens is that you are reducing the size of the array by 1 each loop, and doing it 200 times (or trying to at any rate).

However, once you get to index 100, the array size is also 100 and so trying to access index 101 would be impossible as it can't exist.  Hence you get an "out of bounds error".

What you should be using is :

Code (glbasic) Select
FOR i = 0 TO 199
DEBUG i+" "+BOUNDS(Array[],0)+"\n"
DIMDEL Array[],0
NEXT
Title: Re: Dimdel problem
Post by: Asmodean on 2012-Aug-28
Many thanks. Now it works.
I overlooked that dimdel reindex the Array. Now it make  sense why 'dimdel array[], -1' worked

Many thanks again.
Title: Re: Dimdel problem
Post by: MrPlow on 2012-Aug-28
Hi

My nested loops question was similar ... and I got lot of help.
I was dimdel-ing inside the loop.

FOREACH with a DELETE
or
For n = highest to 0 step -1 (reverse array)
or wait until out of the loop and dimdel then.

Just thought u might like to see the other options...
Title: Re: Dimdel problem
Post by: Asmodean on 2012-Aug-29
Thanks for the help, but I only tried to make a performance-test with arrays. I looked how much CPU usage  dimpush, dimdel and delete used compared to overwriting values in a static array.   
I overlooked the reindexing that is used by dimdel and delete, so I got these error.