• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Intermittent 'Index out of Bounds' type error

C

Carcophan

Guest
Intermittently, I will get a bunch of error string like these in the debug output: "Grid 0, index out of bounds writing [17,24] - size is [32,24]"

Except, 17/24 seems to be within 32/24. No?

Without more debug information I am having a hard time pinpointing where in the code this could be coming from. In light of that, and not wanting to dump all the source here, does anyone have any pointers I could look into?

The resources I have come across so far don't seem to be the root of the problem.
 
Grid indexes start from 0. So a grid with a size of 24 actually is indexed by the numbers 0-23. (24 values...but because index starts at 0, only goes to 23, index number 23 IS the 24th value)

Somewhere your code is trying to use an index value of 24, which is just outside the valid index range of 0-23.
 

Yal

🐧 *penguin noises*
GMC Elder
It's also worth pointing out that for 2D arrays, you can have non-rectangular shapes (even if the array obstensibly is size 30x40, the 8th subarray might be size 1 because you forgot to allocate 40 elements for it)... setting mydata[30,40] to 0 will create 29 arrays of size 1 and one of size 40. So when working with arrays, make sure to always allocate all elements you need before you start operating on the array (unless, of course, you want to dynamically resize it).
 
C

Carcophan

Guest
make sure to always allocate all elements you need before you start operating on the array
Could you (or anyone) elaborate on this a little further, please?

I have reread your post a few times but still don't fully understand it in the context of my skill level.

What I am working with for this example:
Code:
//The room W and H are 1024/768 respectively, hard set through the rooms property settings/room editor 
width_ = room_width div CELL_WIDTH;  //Cell_W and H are each set to 32 in a script. 
height_ = room_height div CELL_HEIGHT;
global.gridA = ds_grid_create(width_, height_);
So width_ = 1024 / 32 = 32 and height_ 768 / 32 = 24. My grid should be 32/24. However, I am getting things like:

Grid 0, index out of bounds writing [191,16] - size is [32,24]
Grid 0, index out of bounds writing [-82,17] - size is [32,24]

So your comment makes sense within the context of '0 will create 29 arrays of size 1 and one of size 40', but how is it deriving the '-82' and '191' etc?
 

FrostyCat

Redemption Seeker
The size of the grid is as you expected, but the position you are trying to write into is not. Read the error message again:
Code:
Grid 0, index out of bounds writing [191,16] - size is [32,24]
Look in your code for ds_grid_set() calls or [# ] accessors and make sure the expressions inside evaluate to something in range, i.e. 0-31 for the first index and 0-23 for the second index.

Rule #1 for arrays, lists, grids and all other zero-indexed things: A size of n implies an index range of 0 through n-1.
 
C

Carcophan

Guest
Wow.

check [# ] accessors and make sure the expressions inside evaluate to something in range, i.e. 0-31
So there is this crazy logic to randomize the map and change wall directions, which uses something like: grid_[# _x, _y] = 10;. The math, which I broke down manually on paper, was feeding _x/_y values that were greater than the grids 32/24 value. I thought the TEN (10) had to be within the range, but the _x and _y, per your suggestion, are out of the range.

Game 'works' as-is with the error in the debug console, but I guess I will end up reworking the code to account for and fix this issue one day soon, to avoid memory leaks or other kinds of issues.


(Real world suggestion to anyone else who is new and is reading this - Work through your formulas and calculations on paper, not just in your head!).
 

Yal

🐧 *penguin noises*
GMC Elder
Could you (or anyone) elaborate on this a little further, please?
Code:
//This will work
for(c = 32;c >= 0; c--){
  two_dimensional_array[c,24] = 0;
}
myvar = two_dimensional_array[22,22]

//This won't
other_array[32,24] = 0
myvar = other_array[22,22]
 
Top