Is this a bug?

Very simple test
**New Empty Gig **
Current Rackspace Script Editor

Initialization

var
    a : Integer
    MyArray : Integer Array
    
    a = 0
    While a < 256 do
        MyArray <-- a
        a = a + 1
    End
        
    Print(MyArray[255])
    
End

Compile

Rackspace (Script Entity: Rackspace) - Array index out of bounds: index (zero-based) = 255: Current array length = 255
Error near line 12 in file: Main

The error message is very clear. With 255 values in your array, with an array starting at index 0, the last element of the array is MyArray[254] not MyArray[255]. The index 255 is out of bounds.

1 Like

But I’m adding 256 values to the array
from 0 to 255

Slight variation but still an error

Initialization

var
a : Integer
MyArray : Integer Array

a = 0
While a < 256 do
    MyArray <-- a
    Print(MyArray[a])
    a = a + 1
End

End

Oh sorry, I read your code a bit too quickly, you are right.

What if you define?

Var MyArray : Integer[256]

And replace MyArray <— a by MyArray[a]=a ?

Yes that works, but unfortunately I need to use a dynamic array as I want to process the results from the GetGPPresetList function, and I don’t know how many entries it will return in advance (but I know it’s limited to 256).

As far as I remember, internally, GP always uses 256-sized arrays even if you define only a few values in it. So it won’t change anything for you.

But you are right, you should not get an error…

You’re right, I just needed to come it this from a slightly different direction.
Thanks.

I think this is a little buglet in the Append operator.

1 Like

Just use the older version

function AppendInteger (a : Integer Array, value : Integer)

It is deprecated but it is still there.

I’ve just fixed this for the next update - good catch

2 Likes

Thanks, I think I prefer that as a stopgap until the fix comes

Unfortunately the problem persists when using AppendInteger

Initialization

var
a : Integer
MyArray : Integer Array

a = 0
While a < 256 do
    AppendInteger(MyArray, a)
    a = a + 1
End

Print(MyArray[255])

End

Rackspace (Script Entity: Rackspace) - Warning in “Main”: Line 9, Col 9: (Script: Rackspace (Script Entity: Rackspace) - ) Function AppendInteger is deprecated: Use ← operator instead
Rackspace (Script Entity: Rackspace) - Array index out of bounds: index (zero-based) = 255: Current array length = 255
Error near line 12 in file: Main

Oh yeah - sorry — the same underlying code is being called in both situations.

See my private PM to you