This would be an example of undefined behaviour. The C++ standard contains many sentences like this: "If (some condition), the result is undefined behaviour".
Why does undefined behaviour exist? Doesn't it just make things more complicated? Yes it does unfortunately make things more complicated for the programmer. The reason for UB is to simplify the job for the compiler writers -- either to make it easier (or possible at all!) to write the compiler in the first place, or to make it easier for the compiler to implement optimisations by making certain assumptions.
Basically, there are many rules that you are supposed to abide by when writing a C++ program. Some violations of these rules can be easily detected at compile time by the compiler, and for these the language requires the compiler to issue an error message. An example of this would be a syntax error.
But there are also violations that are difficult or impossible for the compiler to detect. An example of this would be a piece of memory that you continue using after you have called delete
on it. It's actually theoretically impossible to write a program that detects every possible such violation. So rather than ask the compiler writers to solve the unsolvable problem of detecting these violations, the C++ standard simply declares that your program will experience "undefined behaviour" if you do this. Which means that you can't make any prediction of what will happen -- in fact the standard example is that UB could cause demons to fly out of your nose.
[EDIT] Actually, as litb points out, your specific case is not an example of undefined behaviour but of implementation-defined behaviour, which is slightly more civilised. :) This category describes cases where the authors of the C++ standard recognise that some sort of consistent behaviour can be reasonably expected by the programmer and is not too hard for the compiler writer to implement, but it would be onerous or counterproductive to require this behaviour to be the same on every implementation. (For example, sizeof (int)
is implementation-defined -- although it's often 4 or 8, as far as the C++ standard is concerned, the only requirement is that it's >= sizeof (short)
.) For a compiler to claim standard conformance, it must document the specific behaviour that will occur for every scenario marked as "implementation-defined behaviour" in the standard.