Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are temporary object stored?

Is it true that temporary objects are stored in dynamic (heap) memory?

like image 917
Mihran Hovsepyan Avatar asked Feb 02 '12 09:02

Mihran Hovsepyan


People also ask

Where the temporary objects are created?

3. Where the temporary objects (created while return by value) are created? Explanation: The temporary object are created within the function and are intended to return the value for specific use. Either the object can be assigned to another object or be used directly if possible.

What are temporary objects C++?

A temporary object is an unnamed object created by the compiler to store a temporary value.

What is a temporary object in Java?

Temporary objects are those that have a short lifetime and generally serve no useful purpose other than to act as containers for other data. Programmers generally use temporary objects to pass compound data to -- or return it from -- a method.


1 Answers

The standard does not mandate any memory area (heap/stack) for them, but they are just like local variables "automatic storage", that is at the end of the expression (or longer when bound to a ref-to-const) they are destructed.

Most implementations will store them on the stack just like local variables.

edit:

As James Kanze pointed out: In the case the lifetime of a temporary is extended via a ref-to-const, its store location is on most implementations somewhat determined by the storage location of that reference. That is, in the case of the reference being in static storage, the temporary will be too (just confirmed on gcc). (although IMHO while this is still a temporary in the standards sense, it is arguable whether this is a temporary in the intuitive english sense of that word)

like image 121
PlasmaHH Avatar answered Nov 05 '22 19:11

PlasmaHH