I wanted to know how the following works @ compiler level.
int const iVal = 5; (int&)iVal = 10;
A bit of m/c or compiler level answer would be great full.
Thanks in advance.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It is undefined behavior.
In the first line you define a constant integer. Henceforth, in your program, the compiler is permitted to just substitute iVal with the value 5. It may load it from memory instead, but probably won’t, because that would bring no benefit.
The second line writes to the memory location that your compiler tells you contains the number 5. However, this is not guaranteed to have any effect, as you’ve already told the compiler that the value won’t change.
For example, the following will define an array of 5 elements, and print an undefined value (or it can do anything it wants! it’s undefined)
The generated assembly might look something like: