Search code examples
c++heap-memorymove-semanticsallocationstack-memory

Where are temporary objects allocated in C++?


SomeClass a, b, c;
SomeClass foo();
SomeClass a = (b + c); //Where is the object (b + c) allocated?
SomeClass a = foo(); //Where is the returned value of foo() allocated?

My guess is that they are allocated on the heap because I read that temporary objects are destroyed at the end of the expression(;).

It makes sense to me because the move constructor could be implemented by stealing the pointer of the temporary object on the heap.


Solution

  • If created at all (consider optimizations), they're in automatic storage. I.e. the stack.