[Solved] Access to out of array range does not give any error [duplicate]


Because this is legal C++.
You can try to dereference any pointer, even if it’s not allocated by your program, you can try to access any cell of an array, even if it is out of bounds, the legality of an expression doesn’t depend on the values of the variables involved in that expression.
The compiler doesn’t have to run any static analysis to check whether you’ll actually cause undefined behaviour or not, and shouldn’t fail to compile if it assumes that you will (even when it is obvious that you will).
The problem is that you can’t check all possible array access at compile-time (that would be way too expensive), so you’d have to arbitrarily draw a line somewhere (the problem being the word “arbitrarily”, that wouldn’t fit well in the standard).
Hence, checking that you won’t cause undefined behaviour is the responsability of the programmer (or of specific static analysis tools).

3

solved Access to out of array range does not give any error [duplicate]