Trying to learn lvalues
, rvalues
and memory allocation for them. So with a lot of learning materials there is a bit of chaos.
An rvalue
is a value that needs to exist only in bounds of a expression where it was created (until C++11 at least). So it has an address and block of memory that it occupies. But by definition we cannot get an address of rvalue
, because it is a temporary object in contrast to an lvalue
.
But even before C++11 we were able to get an address of rvalue
by returning it from a function and saving it into a const reference type (uh, I guess not an address but a value).
So, more precisely, how does rvalue
allocation work? For how long does the program or OS really remember that place of memory where the rvalue
was created and marked as allocated and another object cannot take its place?
How I see it, now rvalues
are stored just like lvalues
but we simply have other rights to access them. And they have other types of deallocation - for lvalues
going out of scope, for rvalues
may be optimized by existence in bounds of expression or until there is no more links to it.