Let say I have the following code
int main(){
new int; // Is this expression l-value or r-value??
return 0;
}
I know that lvalues are persistent object (since it has specific place in memory from where we can access latter even after the expression ends) and rvalues are temporary object (it has no place in memory and vaporizes after the expression ends).
I saw some where that it is rvalue expression. How can it be rvalue if the expression returns an address ( a specific place in memory). Or Is it rvalue because what ever the expression new int returns (an address value), it vanishes and could never be caught after the expression ends.
new int
is an r-value.
You can't use:
int i;
(new int) = &i;
Perhaps you are thinking of the object whose address is returned by new int
.
The expression denoting the object whose address is returned, *(new int)
, is an l-value. You can use:
*(new int) = 10;
That's not good code. It leaks memory, but it is legal code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With