Answer the question
In order to leave comments, you need to log in
I can assign a value to a non-existing array element and output it, what's the problem?
I decided to write a program, some question arose, and I decided to check something (namely, assign a value to a non-existent element). As a result (see screenshot), I not only managed to do it, but even brought it out!
Is this a new feature or has someone already solved this problem? I thank you in advance
Answer the question
In order to leave comments, you need to log in
No, you just wrote to a memory area outside of the space allocated for the array. This is called buffer overflow and can lead to unpredictable consequences - for example, if another important variable is allocated immediately after the array, it can be overwritten. Lack of checking user input for valid boundaries can be a serious security vulnerability - local variables are allocated on the stack, including arrays. Going beyond the array will allow overwriting the return address (which is also located on the stack) and, in theory, executing arbitrary code.
In order to prevent this behavior in higher-level languages, the bounds of available memory are checked with each assignment, preventing you from "shooting yourself in the foot". In C++, this is not the case for the sake of greater performance.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question