Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the expression that allocates memory rvalue expression or lvalue expression?

Tags:

c++

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.

like image 353
pokche Avatar asked Aug 08 '16 04:08

pokche


1 Answers

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.

like image 146
R Sahu Avatar answered Nov 18 '22 16:11

R Sahu