1

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?

Ninetou
  • 704
  • 1
  • 7
  • 15
  • 4
    It's "undefined" behavior. What happens when you address outside the bounds of an array is undefined. In this case you get a (garbage) value. In another case you might get a program trap. In an extreme case, in some environments, the entire computer could crash. (So don't do it.) – Hot Licks Jan 02 '14 at 01:00
  • @Mitch You are allowed to create a pointer that points one past the last element. You cannot index the first element past the end. This is still undefined behavior. – IInspectable Jan 02 '14 at 01:47
  • @ IInspectable: that's what my 'loose' N+1 was referring to. – Mitch Wheat Jan 02 '14 at 01:54
  • @Mitch The moment you say *N+1* and *index* you are no longer constructing just a pointer, but also dereferencing it. This is undefined behavior. In other words, if you have `int a[2];`, then `a+2` is legal, but `a[2]` is not. – IInspectable Jan 02 '14 at 02:06
  • Yes. I know all that. I was footloose with my description. But thanks for your concern. – Mitch Wheat Jan 02 '14 at 02:07

3 Answers3

3

There's no array bound checking in C++. What you are doing is reading some garbage values from the stack. It's an undefined behavior to do this.

user2345215
  • 637
  • 5
  • 9
1

It's just ghost data. Nothing useful or reliable. Don't do this.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

What you're seeing is undefined behaviour. C++ doesn't do bounds checking on your array like other languages like Java and C# do. If you give an invalid array index, it still points to a piece of memory, just not memory that you've allocated to the array.

That being the case, if you give an out-of-range array index, it won't throw an exception, it will just do something random (print garbage, segfault, etc). In your case it's not crashing but instead printing the contents of the memory at that index, formatted as an int.

Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65