0

I am in the process of learning C++ and I ran into a "Stack Smashing" error when I attempted to create a simple multiplication table using an array of integers (as shown below). The code executes successfully, but at the bottom I am told "Stack Smashing Detected".

Does anyone know what could be causing this error?


int timesTable[10][10] = {};

for (int i = 1; i < 11; i++) {
    for (int j = 1; j < 11; j++) {
        timesTable[i][j] = i * j;
        if (j == 10) {
            cout << timesTable[i][j] << endl;
        }
        else {
            cout << timesTable[i][j] << ", " << flush;
        }
    }
}

return 0;
DarthQuack
  • 1,254
  • 3
  • 12
  • 22
Ken
  • 23
  • 2

1 Answers1

2

Arrays in C++ are indexed from zero, so your loops should be from 0 to 10 instead of from 1 to 11. Your current program goes out of array range and thus exhibits undefined behavior.

Eugene
  • 6,194
  • 1
  • 20
  • 31
  • Wow. I definitely should have seen that. lol Thanks for the help! You were right on the money. – Ken Dec 22 '20 at 21:34