Apart from where on the stack
or heap
, you should think about it in a c++ way, and that is by considering its storage duration. For example, the global new
operator can be overloaded to do whatever. Maybe the object you are being returned is not on the heap or the stack, but is created in global or some specific device memory.
a
has automatic storage duration. Which means:
The storage for the object is allocated at the beginning of the enclosing code block and deallocated at the end.
The object created by new int
has dynamic storage duration:
The storage for the object is allocated and deallocated per request by using dynamic memory allocation functions.