And why wouldn’t I? In a certain way in C arrays are pointers, not exactly, but almost. Then it can access all available memory. It is the programmer’s problem not to let this happen.
I spoke in C and in fact the question has the tag. But since C++ is compatible with C, it is also valid. What a lot of people don’t understand is that C++ prefers more abstract, higher-level forms that encapsulate security checks and don’t let you do that. The array raw C which, in fact, is a pointer, should not even be used in C++, has better structures.
So vetor[4]
it’s actually the same as saying *(vetor + (4 * sizeof(int))
. What prevents 4000 instead of 4? Nothing. If it gives a value inside the virtual memory, it is valid. Obviously it is corrupting memory.
If you want something more secure, just use the safe parts of C++, and pass away from C, or go to a language that always gives you security, like Java or C#, just to stay in mainstream that most approach C++. C and C++ is for those who want great powers and can have great responsibilities.
Very enlightening. Thank you very much, man. I’m learning c++ pq is being given in the Data Structure class on campus. Thanks for the language tips!
– Elder Son