The term delete has special meaning in C ++, so the use of remote failure.
MyObject my_object = MyObject(0);
This line declares an object of type MyObject created with automatic storage time (i.e., on the stack). This object will be destroyed (i.e., its destructor will be executed) upon completion of the area. The Standard does not provide any conditions for storing associated memory (see the following example).
This object of type MyObject will be constructed using the expression MyObject(0) . This constructor will initialize the memory that was allocated for its exclusive use.
Note: in fact, a temporary can be created and then the copy constructor is called, but most compilers avoid this intermediate step, fortunately, since the standard specifically allows it.
my_object = MyObject(1);
This line assigns the new value, defined by the expression MyObject(1) , to the existing my_object . To do this, a temporary type MyObject with automatic storage time is created. Then the assignment operator is executed; if it is not overloaded, it will copy the temporary state to my_object , deleting the old state. At the end of the expression, the temporary is destroyed (once again, no evidence is made to remember the associated memory).
Note: MyObject(0) not βdeletedβ because it does not exist, instead of the memory in which it wrote its state, is reused to copy the state from MyObject(1) .
As promised, as this seems like your concern, a discussion of aspects of memory. This is compiler specific, but most compilers behave the same way.
Suppose we have the following function:
void f() { MyObject my_object = MyObject(0); { my_object = MyObject(1); do_something(my_object); } { my_object = MyObject(2); do_something(my_object); } }
How much stack space is required?
- Suppose it does a direct build on the first line
- We assume that the compiler is not smart enough to do Stack Coloring (e.g. Clang is not working)
Under this assumption, space is required for 3 MyObject .
MyObject my_object = MyObject(0); : my_object needs to live until the end of the functionmy_object = MyObject(1); : need to create a temporary need.my_object = MyObject(2); : need to create a temporary need.
The stack space is remembered at the end of the function.
If the compiler is smart enough to do Stack Coloring, then two temporary files (which are never needed together) can use the same memory spot, thereby reducing the space requirement to 2 MyObject .
The smart optimizer can also possibly directly build MyObject(1) and MyObject(2) directly into my_object (if it can prove that the effects will be the same as creating a temporary one and then copying it later), thereby reducing the required space to 1 MyObject .
Finally, if the definition of do_something visible and does not use its parameter, then under certain conditions it (theoretically) could completely bypass the construction of my_object . Such optimizations can witness simple programs, such as:
int main() { int i = 0; for (; i < 1000; ++i); return i; }
which are trivially optimized for:
int main() { return 1000; }
(Notice how i disappeared)
As you can see ... it is actually very difficult to guess what the compiler / optimizer can do. If you really have strict memory requirements, then (perhaps surprisingly) the best option would be to replace the blocks by function.