Array out of bounds error

Hi all,

So, I have a fairly simple code snippet based on the KITT example that every so often triggers “Array out of bounds” error in the following chunk:

for (i= lastLeader; i < leader; i++) {
    pixels[i % pixelCount] = kittBrightness // error occurs here
    if (i % pixelCount == lastLeader - 1) {
        break
    }
}

Vars lastLeader and leader are essentially irrelevant with the only prerequisite being that leader is ahead of lastLeader, which (in this particular version) it is since the direction in the code is always positive (as reflected also in the movement of the dot). I cannot see why the pixel array would ever be out of bounds since the modulo should always ensure that the value is between 0 and pixelCount - 1 (modulo of pixelCount should be 0). Any idea why this would be the error PixelBlaze throws? Is this possibly a bug in the PixelBlaze itself? Is there a way to print out the value of a particular variable (in this case i and pixelBlaze) at the time the error has occurred?

Additional info: I have made sure that the number of pixels on the settings tab is correct and this is also reflected in ensuring that all LEDs on the strip are engaged.

Thank you for your help.

Hi @ico!

I have a theory but it would help to see the entire pattern code.

If you were advancing leader or lastLeader by 1 forever, it might overflow to a negative number, which would pass a negative array index.

I’ve been in your shoes before - getting this error and swearing something was up with Pixelblaze itself :slight_smile:

1 Like

Thanks, Jeff. I specifically make sure after each update to truncate leader to leader%pixelCount, so I am fairly certain, it should never overflow like that. The line immediately below is

if (leader >= pixelCount) leader = leader % pixelCount

I will try to clean-up my code and then share it.

Also, is there a way to print out the values and freeze things when an error like this one occurs?

Far as I know, there’s no way to stop execution and look at a variable, but to figure out what’s going on you can temporarily add some exported variables to your code, and watch them:

export var iMin = 32000;    // large number
export var iMax = -32000; // small number
.
.
.
for (i= lastLeader; i < leader; i++) {
    if (i > iMax) iMax = i;
    if (i < iMin) iMin = i;
    pixels[i % pixelCount] = kittBrightness // error occurs here
    if (i % pixelCount == lastLeader - 1) {
        break
    }
}

This will tell you at least in which direction it’s indexing outside the array boundary. I wind up doing this sort of thing pretty frequently. Yes, it’s clunky, inconvenient and slows things down, but it’ll give you information on what’s going wrong. (Also, check to make sure that pixelCount isn’t getting accidentally assigned a value in the pattern somewhere.)

2 Likes

@ico,
Is lastLeader ever negative? The leader var would be negative in the original KITT code briefly, when it is traveling left (toward 0), before the bounds checking code kicks in:

  if (leader < 0) {
    direction = -direction
    leader = 0
  }

Without that in place (posting your whole pattern would help), leader could be negative. If leader was left negative, and this ran:

lastLeader = floor(leader)

Then lastLeader would be a negative integer (probably -1).
The mod of a negative dividend and a positive divisor is negative: -1 % 100 == -1

Expanding on @zranger1’s idea, and answering your question about seeing a value I do a similar trick to debug, where I conditionally set an exported variable to see things when there’s an unexpected state. e.g.:

export var badVal = 0

//...

for (i= lastLeader; i < leader; i++) {
    index = i % pixelCount
    if (index < 0 || index > pixelCount - 1) {
        badVal = index
    }
    pixels[index] = kittBrightness // error occurs here
    if (index == lastLeader - 1) {
        break
    }
}
3 Likes

Thank you @jeff and @zranger1 for your assistance. This is indeed very helpful. There was no change in direction (I removed that part of the code). Strange, as I continued to clean the code, the error disappeared. Once the code snippet is completely cleaned-up, happy to share.

1 Like

@zranger1 thanks for the tip. I started doing that, as well, just so I can have a better idea of not only what is going on, but also to understand better the analog value’s behavior. Thanks again for your help.