6

I alloced an int array of 3 elements, and thought of this code below:

int a[3];
for(int i = -2; i < 3; ++i){
    a[i] = i; 
    cout<<a[i]<<" ";
}

Here's its output:

-2 -1 0 1 2 

It seems like array a has 5 alloced space, and a is at the middle of those space. Any ideas?

user8600953
  • 121
  • 1
  • 1
  • 2
  • 16
    C++ have no array bounds checking. Indexing out of bounds, in either direction, leads to [*undefined behavior*](http://en.cppreference.com/w/cpp/language/ub). – Some programmer dude Nov 08 '17 at 02:45
  • The question suggested by @SerkanPekçetin doesn't *specifically* mention negative indexes, but the principle is pretty much exactly the same. – Daniel H Nov 08 '17 at 02:57

3 Answers3

40

To explain how negative indexes work, you first have to learn (or remember) that for any array or pointer a and index i, the expression a[i] is equal to *(a + i).

That means that you can have a pointer to the middle element of an array, and use it with a positive or negative index, and it's simple arithmetic.

Example:

int a[3] = { 1, 2, 3 };
int* p = &a[1];  // Make p point to the second element

std::cout << p[-1];  // Prints the first element of a, equal to *(p - 1)
std::cout << p[ 0];  // Prints the second element of a, equal to *p
std::cout << p[ 1];  // Prints the third element of a, equal to *(p + 1)

Somewhat graphically it can be seen like

+------+------+------+
| a[0] | a[1] | a[2] |
+------+------+------+
^      ^      ^
|      |      |
p-1    p      p+1

Now when using a negative index in an array, as in the example of a in the question, it will be something like this:

+-----+-------+-------+------+------+------+
| ... | a[-2] | a[-1] | a[0] | a[1] | a[2] |
+-----+-------+-------+------+------+------+

That is, negative indexes here will be out of bounds of the array, and will lead to undefined behavior.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 6
    This is all true in an of itself, but ignores the fact that in the question the "negative" indices being used aren't within the allocated array, and the behaviour's undefined. – Tony Delroy Jul 08 '19 at 15:11
  • 1
    @TonyDelroy Which I addressed in my comment to the question itself. – Some programmer dude Jul 08 '19 at 15:16
  • It's a shame, because it's a great answer and exactly what I came here looking for. Please read questions carefully before answering. – kendfss Oct 12 '21 at 20:25
2

That is something you should never ever do! C++ doesn't check the boundaries of built in plain arrays so technically you can access locations which are out of the allocated space (which is only 3 ints not 5) but you will ultimately produce errors.

pTz
  • 144
  • 3
1

When I ran this on my own IDE (Visual Studio 2017) it threw an exception stating that the stack around the array had become corrupted. What I think is happening is that 3 spaces in memory are being allocated but you force the array to write to five spaces, those preceding and after the initial allocation. This will work technically, but is definitely not recommended. You are basically writing over top of something in your memory and that can have bad consequences when done in large arrays.