This program crashes on my system:
Code:
int main()
{
int matrix[124];
int i;
for (i = 0; i < 126; ++i)
{
//matrix[i] = i % 2;
matrix[i] = i;
}
}
The reason is (probably) because I am exceeding the boundaries of matrix. It will not crash when I iterate to i<125, which one would think it would. Anyhow, not knowing enough about Linux internals, I suspect the code crashes because it is trying to reach a value outside its program space.
As for your code, the reason it does not crash, nor terminate, is because the iterator i is being reassigned a value, presumably either 0 or 1, after you exceed the array boundaries. The variable i falls right the matrix array on the stack.
Anyhow, it is good programming practice to avoid using hard-coded numerals when declaring arrays or when using them as boundaries in a conditional. Declare these fixed-numbers with a const unsigned int. Alternatively, compute the number of elements in the array as the limiting value.
Examples:
Code:
static const unsigned int SIZE = 124;
int matrix[SIZE];
...
for (unsigned int i = 0; i < SIZE; ++i)
{
...
}
Code:
static const unsigned int SIZE = 124;
int matrix[SIZE];
...
for (unsigned int i = 0; i < sizeof(matrix)/sizeof(int); ++i)
{
...
}
Bookmarks