0
int test[3] = {1,2,3};
cout<<test[3]<<endl;    
// this will get a error

but

int test[3] = {1,2,3};
int (*A)[3];
A = &test;
cout<<test[3]<<(*A)[3]<<endl;  
// this will print -858993460 withiout any errors

So could anyone tell me why? i am really confused by that.

in the first case why it is not outofboundary error but an undefined error? and why the second case will not get a error? i used to think they are the same...

actually i did know the array start from 0, i am confused by why the first got an error but the second won't????

2
  • You are referring to the 4th location in your array instead of the 3rd. You should be printing test[2] to get value '3'. Commented Aug 26, 2011 at 14:24
  • Note that in C++, arrays are 0-based. Commented Aug 26, 2011 at 14:25

3 Answers 3

10

You are invoking undefined behaviour. In both cases, you are indexing beyond the bounds of the array; the corresponding behaviour is undefined. It might crash, or it might output nonsense, or it might do something else.

Sign up to request clarification or add additional context in comments.

1 Comment

+1. So to make it short: don't ever ever do that again ! :)
3

Undefined behaviour seems a very tricky concept for newbies to understand. But it's very simple, if you break the rules of C++ (as both your examples do) then, very often, the behaviour of your program is undefined. It means exactly what it says and it's pointless asking 'why does it do that' as there are no longer any rules or laws applicable to your situation.

6 Comments

If I were writing a C++ compiler, I would be very tempted to cause certain common undefined behavior (like writing past the end of a buffer) to do something catestrophic like delete the OS. It would encourage careful programming.
Someone should write a compiler, for teaching, which fails reliably on undefined behaviour. It would be like riding a bike with stabilisers.
First of all, you are not breaking C++ laws by going out of bounds of an array (if you were that would have been a compile error). Also, going out of bounds of an array is absolutely NOT undefined behavior. If you are aware of your compiler, you can deterministically say what will happen. That is the principle behind buffer overflow attacks on software. And again, all rules and laws of how the processor executes your program hold and this statement is false: ... as there are no longer any rules or laws applicable to your situation
@john the problem with that (Java as Daniel mentioned) is that, that compiler for teaching, which is really slow and inefficient would become popular because programmers wouldn't learn to be correct, but rather rely on the compiler to correct them and that would create a lot of mindless programmers creating bad software (just like what is happening now with java and java programmers)
@Shahbaz: You are right that if you are aware of your compiler, you can deterministically say what will happen if you go out of bounds of an array. That is the definition of undefined behavior. The Standard does not say (define) what should happen, and the choice is left up to the implementation. Check out the wikipedia article about it
|
2

Array indices in C/C++ start from 0. Therefore an array of size 3, has valid indices 0, 1 and 2.

Right now, you have your variable test on the program stack. Therefore, the memory outside the bound of the array is still yours (that is why you don't get a segmentation fault (access violation) error when you are reading index 3 of your array). However, you are reading part of the stack that is gibberish from your previous function calls, resident gibberish on the memory or other variables you have in the function. That is why you are getting weird numbers as a result.

You should beware of going out of array bounds. In a complicated program, that is really hard to debug, because you go over the stack changing other variables and all you see when debugging is random behavior in variables you didn't even touch!!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.