-1 array value?!

can please, anyone explain to me, why the hell does this code works?

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
	int arr[12];
	arr[-1] = 2;
	cout << arr[-1];
	cin.get();
}


so does this

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
	int arr[12];
	arr[-5] = 2;
	cout << arr[-5];
	cin.get();
}


and so on...
Last edited on
array subscript are equivalent to pointer arithmerics: a[x] = *(a + x). It is completely legal to subscript with negative numbers. However if variable you applying subscript operator is not pointing to the middle of the array (so resulting pointer would point to the valid element) it will cause undefined behavior.
You got lucky and nothing breaks. If you add more code, recompile your program or moon phase changes, your program can crash, output something unexpected or summon demons from your nose.
Your code works, but it is not legal.

Following is legal BTW:
1
2
3
4
int arr[15];
int* p = arr + 10;
p[-4] = 6;
std::cout << -4[p];
Last edited on
No bounds checking in C arrays
Last edited on
@MiiNiPaa
I dont really understand what you just said, but thanks anyway

@mobotus
but if im not mistaken I even couldnt do

1
2
int arr[12];
arr[15] = 123;


few years ago, as it crashed if I remember right, how come now it is absolutely possible?
how come now it is absolutely possible?
You got lucky and did not corrupt critical memory.
Look how unrelated memory can be changed by your array modification:
http://coliru.stacked-crooked.com/a/7883b309e52ca6a4
1
2
3
4
5
6
7
8
9
#include <iostream>

using namespace std;
int main ()
{
     int a = 0, x[3], b = 0;
     x[-1] = 1;
     cout << a << b;
}
01
C++ does not babysit you. It assumes that you know what are you doing and will not stop you from doing dangerous things.

A link on somewhat related topic: accessing what you should not
http://stackoverflow.com/a/6445794/3410396
Last edited on
wait, shouldn't "a" have value of 1? as it was created before "x" array, and therefore x-1 was accessing its memory?
wait, shouldn't "a" have value of 1? as it was created before "x" array, and therefore x-1 was accessing its memory?


The point is that you are accessing out of bounds memory, and the result is that you are corrupting other memory.

If it seems unpredictable or unintuitive, that's because it is. This is a very bad/dangerous thing to do and you can't always predict what will happen.
Last edited on
How do you know that? a's value could be anywhere. For all you know b's value could have been put before the array values.
wait, shouldn't "a" have value of 1? as it was created before "x" array, and therefore x-1 was accessing its memory?
Compiler can place variables in memory how it likes. In fact, if I turn o n optimisation it will output 00, because we do not change these variables (by any legal way) and compiler just throws them out and uses their values directly.
And there is fun thing about UB: compiler can do anything if it encounters undefined behavior. In my case x[-1] got thrown out too and code was simplified to cout << 0 << 0;

Some links to understand why UB is bad:
http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html
http://blogs.msdn.com/b/oldnewthing/archive/2014/06/27/10537746.aspx

Also:
1
2
3
4
5
6
7
8
9
10
11
12
13
int* foo() {
    int i = 3;
    return &i; 
}
void bar() {
    int k = 8;
}

int main() {
    int *i = foo();
    bar();
    std::cout << *i ;
}
This code can output 0, 8 or 3, depending on compiler settings. Maybe I can get other numbers if I would launch it today, I do not know.
@MiiNiPaa I like the foobar2k reference

also thanks all for responses

edit: k I got it
Last edited on
Topic archived. No new replies allowed.