And why would not it? In a way in C arrays are pointers , not exactly, but almost. Then it can access all available memory. It is the developer's problem not to let this happen.
I spoke in C and in fact the question has the tag . But since C ++ is C-compliant, that's fine too. What most people do not understand is that C ++ prefers more abstract, more high-level forms that encapsulate security checks and do not let it do that. The raw array of C which, in fact, is a pointer, should not even be used in C ++, has better structures .
So vetor[4]
is actually the same as *(vetor + (4 * sizeof(int))
. What stops being 4000 in place of 4? Anything. If it gives a value within virtual memory, it is valid. Obviously it is corrupting memory.
If you want something more secure, just use the secure parts of C ++, and pass away from C, or go to a language that always gives security, like Java or C #, just to stay in the mainstream which are closest to C ++. C and C ++ is for those who want great powers and can have great responsibilities.