I have been playing around with arrays in C++ and I noticed that if I will tell the program to print a value from index that technically should be out of range, the program is still capable of finding a value for it. How does that happen?
Code:
#include <iostream>
using namespace std;
int main(void)
{
int x = 0;
int array[5][5];
for (int row = 0; row < 5; row++) {
cout << "OutOfRange array index [0][8]: " << array[0][8] << endl;
cout << "OutOfRange array index [2][8]: " << array[2][8] << endl;
cout << "OutOfRange array index [8][8]: " << array[8][8] << endl;
cout << "OutOfRange array index [88][88]: " << array[88][88] << endl;
cout << "OutOfRange array index [90][90]: " << array[93][93] << endl;
for (int col = 0; col < 5; col++) {
array[row][col] = x;
x++;
cout << array[row][col] << " ";
}
cout << endl;
}
return 0;
}
For some indexes it returns a very high value which I assume is a memory address but then for others it seems like it is capable of finding some values (or maybe just a coincidence where memory address is low enough to make me think its a value?).
OutOfRange array index [0][8]: 0
OutOfRange array index [2][8]: 17
OutOfRange array index [8][8]: 0
OutOfRange array index [88][88]: 0
OutOfRange array index [90][90]: 152176
0 1 2 3 4
OutOfRange array index [0][8]: 0
OutOfRange array index [2][8]: 17
OutOfRange array index [8][8]: 2104726264
OutOfRange array index [88][88]: 0
OutOfRange array index [90][90]: 152176
5 6 7 8 9
OutOfRange array index [0][8]: 8
OutOfRange array index [2][8]: 17
OutOfRange array index [8][8]: 2104726264
OutOfRange array index [88][88]: 0
OutOfRange array index [90][90]: 152176
10 11 12 13 14
OutOfRange array index [0][8]: 8
OutOfRange array index [2][8]: 17
OutOfRange array index [8][8]: 2104726264
OutOfRange array index [88][88]: 0
OutOfRange array index [90][90]: 152176
15 16 17 18 19
OutOfRange array index [0][8]: 8
OutOfRange array index [2][8]: 18
OutOfRange array index [8][8]: 2104726264
OutOfRange array index [88][88]: 0
OutOfRange array index [90][90]: 152176
20 21 22 23 24
Program ended with exit code: 0
[2][8]
index is very interesting to me as well. Why does it change its value from 17
to 18
during the very last run of outer for
loop?