0

I wrote a multiplication table like this:

#include <iostream>
#include <conio.h>
using namespace std;
int main(){
    int table[9][9], i, j;
    for (i = 0; i < 10; ++i){
        for (j = 0; j < 10; ++j)
        {
            table[i][j] = (i + 1) * (j + 1);
            cout << table[i][j] << "\t";
        }
        cout << endl;
    }
    _getch();
    return 0;
}

And when I run it it gives me the right answer but when I press a key it throws this error:

 run time check faliure #2-stack around the variable table was corrupted

But it doesn't throw that error when I change the code to this:

......
int main(){
    **int table[10][10]**, i, j;
    for (i = 0; i < 10; ++i){
......

If they both give the same answer then what's the difference??

Robert
  • 10,403
  • 14
  • 67
  • 117

6 Answers6

1

You are overflowing your arrays, The max index in bounds is 8 (since the indices are zero based and you defined 9 cells as your dimensions size so 8 will be the last cell in each dimension) and your for loop can reach till 9 (including 9) which will cause an overflow.

giorashc
  • 13,691
  • 3
  • 35
  • 71
  • but my code with int table[9][9], i, j; is giving the same output as the one with int table[10][10], i, j; the first one just throws an error – aliKianfar Apr 03 '14 at 11:21
  • That is because accessing the overflowed cell will not necessarily crash your program but will cause an undefined behavior as in your case – giorashc Apr 03 '14 at 11:32
1

The snippet int table[9] declares an array of size 9, that means valid indices are actually 0-8. But your loop iterates from 0 to 9. This causes a write into an invalid memory region (which doesn't belong to your array), therefore corrupting your stack.

Now you actually have a two dimensional array, but the problem remains the same.

lethal-guitar
  • 4,438
  • 1
  • 20
  • 40
  • but my code with int table[9][9], i, j; is giving the same output as the one with int table[10][10], i, j; the first one just throws an error – aliKianfar Apr 03 '14 at 11:22
  • @aliKianfar It might be outputting values for the invalid indices, but these are not written to valid memory locations, and therefore cause undefined behavior. They might look correct, but you're still corrupting the stack, which will prevent subsequent code from running properly and cause hard to debug problems (if not caught by the runtime check, as in your case). – lethal-guitar Apr 03 '14 at 12:13
1

You're going outside the array in your for loops. They should be:

for (i = 0; i < 9; ++i){
    for (j = 0; j < 9; ++j)

The array indexes run from 0 to 8, but you were going up to 9 because your conditions were i < 10 and j < 10.

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • but my code with int table[9][9], i, j; is giving the same output as the one with int table[10][10], i, j; the first one just throws an error – aliKianfar Apr 03 '14 at 11:19
1

Rule of thumb: in for loops, N in (i = 0; i < N; i++) clause should (almost always) be equal to the corresponding array's length. When you see either i <= N or i < N + 1, it's (most often) a sign of the dreaded off-by-one bug.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
1

Arrays in C/C++ start with 0 index. When you declare table[9][9] you reserve memory for 9x9 array with indexes 0..8, 0..8 but in for loop your upper index is 9 - it is out of array range

I guess you should declare table like you pointed:

int table[10][10];
INait
  • 181
  • 6
1

You are accessing out of array's range element. Your array is a 9x9 table but you are trying to access 10x10 table. So either use i<9 and j<9 in your both loops or increase your array size to table[10][10]. Hope this might help.

Constantin
  • 8,721
  • 13
  • 75
  • 126
  • but my code with int table[9][9], i, j; is giving the same output as the one with int table[10][10], i, j; the first one just throws an error – aliKianfar Apr 03 '14 at 11:21
  • If you use array out of bound, "undefined behavior" may occur and it is one of those undefined behavior you got for this program.Code result in right output because c++ doesn't have array out of bound check and so you can access out of bound values.But you are overwriting undeclared memory which might be took as corrupted data. You can check out this question of stack overflow for more information : http://stackoverflow.com/questions/1239938/c-accesses-an-array-out-of-bounds-gives-no-error-why – Chirag Bansal Apr 03 '14 at 13:44