D
D
DimaIs2016-12-24 12:39:59
Programming
DimaIs, 2016-12-24 12:39:59

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
0c4d74ed52fc42868dd75e67f8c4f564.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
1
15432, 2016-12-24
@DimaIs

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 question

Ask a Question

731 491 924 answers to any question