The creation of dynamically allocated objects in C++ happens in two stages. The first stage is responsible for allocating sufficient memory to store the object, and the second stage is responsible for initializing the newly allocated chunk of memory, depending on the type of the object being created. Similarly, the destruction of dynamically allocated objects in C++ happens in two stages. The first stage is responsible for finalizing the object, depending on the type, and the second stage is responsible for deallocating the memory used by the object. The C++ Standard, [basic.life], paragraph 1 [ISO/IEC 14882-2014], states:
The lifetime of an object is a runtime property of the object. An object is said to have non-trivial initialization if it is of a class or aggregate type and it or one of its members is initialized by a constructor other than a trivial default constructor. [Note: initialization by a trivial copy/move constructor is non-trivial initialization. — end note] The lifetime of an object of type
T
begins when:— storage with the proper alignment and size for type
T
is obtained, and
— if the object has non-trivial initialization, its initialization is complete.The lifetime of an object of type
T
ends when:— if
T
is a class type with a non-trivial destructor, the destructor call starts, or
— the storage which the object occupies is reused or released.
...
SEI CERT C++ Coding Standard | MEM51-CPP. Properly deallocate dynamically allocated resources |
Bibliography
[ISO/IEC 14882-2014] | Subclause 3.8, "Object Lifetime" Clause 9, "Classes" |
...