int x = 12;
12 is said to be integer literal, and therefore can’t be used in the LValue.
- How does the compiler allocate memory to a literals?
- What is the scope of a literals?
- Why can’t we get its address with an &12 in its scope?
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.
OK Bad example in the question.
But the question is still valid:
Lets try:
Undefined up-to the compiler.
But it would be real easy to allocate a tiny bit more memory onto the stack frame and hold it there. Then destroy it and reduce the size of the stack frame (though this answer makes a whole lot of assumptions about the underlying hardware that you should never do (best just to think of it as the compiler doing magic)).
The temporary object lives until the end of the expression (usually the
;) unless it is bound to a const reference. If it is bound to a const reference then it lives to then end of the scope that the reference belongs too (with a few exceptions (like constructors)).In the question 12 is not a temporay object.
It is an integer literal.