Strange bug where writing to one variable changes another


Today I’ve been banging my head against a wall teying to figure out some odd values for a variable, and I eventually narrowed it down to the fact that writing to one variable with values that I’m reading from EEPROM is also changing a completely different variable. Here’s a snippet:

for (byte i = 0; i < 3; i++) {

senseDistanceSelector = + i);
*Serial.print(senseDistanceSelector[0]); *

  • for (byte m = 1; m < 13; m++) {*
    pressureSelector_[m] = + m + (i * 20));_
    * }*
    *Serial.print(senseDistanceSelector[0]); *
    The first Serial.print gives the correct value for senseDistanceSelector[0], while the second one goves an incorrect value, seemingly taken from the EEPROM being read to set the pressureSelector array.
    Do you see any possible why writing to one variable would change another? I fist thought that it might be a memory issue, but using a couple of functions I found online suggests I have more than 50% free, which also jibes with what the IDE tells me at upload. The processor is a 32u4.
    Thanks for your help!

Please use code tags (</> button on the toolbar) when you post code or warning/error messages. The reason is that the forum software can interpret parts of your code as markup, leading to confusion, wasted time, and a reduced chance for you to get help with your problem. This will also make it easier to read your code and to copy it to the IDE or editor. If your browser doesn’t show the posting toolbar then you can just manually add the code tags:
[code]``[color=blue]// your code is here[/color]``[/code]
Using code tags and other important information is explained in the How to use this forum post. Please read it.

Please always do a Tools > Auto Format on your code before posting it. This will make it easier for you to spot bugs and make it easier for us to read. If you’re using the Arduino Web Editor you will not have access to this useful tool but it’s still unacceptable to post poorly formatted code. I recommend you to use the standard IDE instead.

This sort of bug is likely caused by writing outside the bounds of your array but I can’t be sure without seeing the full program. Often the compiler will warn you about this sort of thing if you set File > Preferences > Compiler warnings to “All”.

Uggh! Re-reading my question above made me realize that I forgot that the pressureSelector array is zero indexed, and I’m causing an array overrun, presumably writing elsewhere in the stack(?). I won’t have a chance to test it until tomorrow...

Thanks pert, we were posting at the same time, but you nailed it with the array overrun. Thanks too for the posting advice—I’ll use tags from now on for sure.