I study the main features of C ++ and here's the code:
int mas[12]; mas[15]=15; cout<<"15 element = "<<mas[15]<<"\n"; For some reason I work. After all, is it going beyond the bounds of the array?
I study the main features of C ++ and here's the code:
int mas[12]; mas[15]=15; cout<<"15 element = "<<mas[15]<<"\n"; For some reason I work. After all, is it going beyond the bounds of the array?
C ++ is an insecure language.
The rules of the language say that it is impossible to go beyond the boundaries of the allocated memory. The responsibility for following the rules rests with you, the programmer. C ++, unlike safe languages, does not check whether you are following the rules, so you will not get an error directly at this point.
But once you broke the rules, any trouble can happen. The standard emphasizes that if you violate the rules, all guarantees of the language are removed.
When reading beyond the bounds of the array, if you are lucky, the memory by offset of 15 element sizes from the beginning of the array is available and contains nothing bad (on Intel’s architecture there is no “bad”, but on others it is ) and if the optimizer is not very smart, you just read some strange meaning.
In case you are less lucky, the program will simply fall.
And in a completely bad case, the program will begin to behave strangely, and in some distant place not connected with this point.
When recording is still worse, some important data structure may accidentally end up at this address (for example, it can be the return address from the current function), and you will overwrite it with some incomprehensible value according to the principle “God sends to”.
Just don't do it, C ++ trusts you.
Source: https://ru.stackoverflow.com/questions/545231/
All Articles