If we have the following code snippet:
MyObject my_object = MyObject(0);
my_object = MyObject(1);
What happens to MyObject(0)? Is it deleted? Looking at what I have read about it it should only be deleted when we leave the scope of creation, so the anwser is probably no. If this is the case is there any way to explicitly delete it other than using pointers?
The term
deletehas a special meaning in C++, so the use of deleted is unfortunate.This line declares that an object of type
MyObjectcreated with automatic storage duration (ie, on the stack). This object will be destructed (ie, its destructor will be executed) when the scope ends. No provision is made in the Standard for the recollection of the associated memory (see later example).This object of type
MyObjectwill be constructed using the expressionMyObject(0). This constructor will initialize the memory that has been set apart for its exclusive use.Note: actually, a temporary could be created and the copy constructor then called, but most compiler eschew this intermediate step, thankfully, as the Standard specifically allows it.
This line assigns a new value, determined by the expression
MyObject(1), to the already existingmy_objectobject. To do so, a temporary of typeMyObjectis created with automatic storage duration. Then, the assignment operator is executed; if not overloaded it will copy the state of the temporary intomy_object, erasing what previous state was there. At the end of the expression, the temporary is destructed (once again, no provising is made for the recollection of the associated memory).Note:
MyObject(0)is not “deleted”, as it does not exist, instead the memory it had written its state to is reused to copy the state fromMyObject(1).As promised, since this seems your worry, a discussion on the memory aspects. This is compiler specific, but most compilers do behave similarly.
Suppose that we have the following function:
How much space does it require on the stack ?
With those assumption, it requires the space for 3
MyObject.MyObject my_object = MyObject(0);:my_objectneed live until the end of the functionmy_object = MyObject(1);: a temporary need be createdmy_object = MyObject(2);: a temporary need be createdThe stack space is recollected at the end of the function execution.
If the compiler was smart enough to perform Stack Coloring, then the two temporaries (that are never needed together) could use the same memory spot, thus lowering the space requirement to 2
MyObject.A smart optimizer could also, possibly, directly build
MyObject(1)andMyObject(2)directly intomy_object(if it can prove that the effects would be the same than building a temporary and then copying it), thus lowering the space requirement to 1MyObject.Finally, if the definition of
do_somethingis visible, and it does not use its parameter, then under certain conditions it could (in theory) completely bypass the construction ofmy_object. Such optimizations can be witness with simple programs like:which are trivially optimized to:
(Note how
idisappeared)As you may notice… it’s actually very hard to guess what the compiler/optimizer will be able to do. If you really have tight memory requirements, then (perhaps surprisingly), your best bet might be to replace the blocks by functions.