Arrays in C++ are indexed from 0, so if you have an array with 2 elements, the legal indexes will be 0 and 1; writing to (the illegal) index 2 will write to memory elsewhere in your program, as you've discovered Amending your declaration of "grid" as below…