Author Topic: V11: REDIM + FOREACH  (Read 3706 times)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
V11: REDIM + FOREACH
« on: 2013-Jan-18 »
If you use REDIM [0] to 'delete' an integer array, array.count gets not updated, so FOREACH thinks the array is still big and calls the deleted entries too. LEN and BOUNDS work correct. The bug only happens with integer arrays and when you REDIM it to [0], all other values work fine, so do arrays of floats, string and types. I did only test V11.

Here is an example:
Code: (glbasic) [Select]
LOCAL a%[]
DIM a[4]
FOR i%=0 TO a.count-1
a[i]=i
NEXT
REDIM a[0]
DEBUG "count: "+(a.count)+"  len: "+LEN(a[])+"  bounds: "+BOUNDS(a[],0)+"\n"
DEBUG "elements with foreach:\n"
FOREACH b IN a[]
DEBUG b+"\n"
NEXT
DEBUG "elements with for:\n"
FOR i%=0 TO LEN(a[])-1
DEBUG a[i]+"\n"
NEXT
This is the output that i get from this example:
Code: (glbasic) [Select]
count: 4  len: 0  bounds: 0
elements with foreach:
0
1
2
3
elements with for:

BTW. did I win a prize for finding the most exotic bug in the new year? :D
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

MrTAToad

  • Guest
Re: V11: REDIM + FOREACH
« Reply #1 on: 2013-Jan-18 »
Quite true - obviously that is an internal count...

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10814
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
V11: REDIM + FOREACH
« Reply #2 on: 2013-Jan-20 »
Oh. If that's true its a good find. These things cause really trouble.

Offline Hemlos

  • To boldy go where no pixel has gone before!
  • Global Moderator
  • Prof. Inline
  • *******
  • Posts: 1621
  • Particle Hawk
    • View Profile
Re: V11: REDIM + FOREACH
« Reply #3 on: 2013-Jan-21 »
Interesting, didnt know there was a DOT count feature.
Is this intended to be quicker than LEN and BOUNDS?
Volume_of_Earth(km^3) = 4/3*3.14*POW(6371.392896,3)

Offline kanonet

  • Administrator
  • Prof. Inline
  • *******
  • Posts: 1137
    • View Profile
    • My GLBasic code archiv
Re: V11: REDIM + FOREACH
« Reply #4 on: 2013-Jan-21 »
Array.count is no official GLB feature/syntax, its a 'hack' that uses GLB intern C transformation. It is faster than LEN/BOUNDS, but I dont know if its save (and if it will be in the future), but I use it quite some time without problems (and since it is used in FOREACH intern, it should be no problem to use it). Obviously it just works with integers, floats and strings, but not with types (I have no idea, how to do this with types...). If you want to do it on a String array, LEN(array$[]) turns into array_Str.count.
Btw. I was always thinking about creating a thread where we could collect all the cool 'hacks' and workarounds, that make life easier...

I posted it here just cause I already tracked down the error to this counter, so Gernot just needs to do a shorter search on his own.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

Offline hardyx

  • Community Developer
  • Prof. Inline
  • ******
  • Posts: 500
    • View Profile
Re: V11: REDIM + FOREACH
« Reply #5 on: 2013-May-21 »
It's not a new bug. Doesn't work in 10.283 version either.