...
When a dynamically allocated object is created by means other than a call to new
, it is said to be manually managing the lifetime of that object. This situation arises when using other allocation schemes to obtain storage for the dynamically allocated object, such as using an allocator object or malloc()
. For instance, a custom container class may allocate a slab of memory in a reserve()
function in which subsequent objects will be stored. See MEM51-CPP. Properly deallocate dynamically allocated resources for further information on dynamic memory management and as well as MEM08-CPP. Use new and delete rather than raw memory allocation and deallocation for the recommendation to use new
and delete
whenever possible.
When manually managing the lifetime of an object, the constructor must be called to initiate the lifetime of the object. Similarly, the destructor must be called to terminate the lifetime of the object. Use of an object outside of its lifetime may result in undefined behavior. An object can be constructed either by calling the constructor explicitly using the placement new operator or by calling the construct()
function of an allocator object. An object can be destroyed either by calling the destructor explicitly or by calling the destroy()
function of an allocator object.
...