Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The C++ programming language adds additional ways to allocate memory, such as the operators newnew[], and placement new, and allocator objects. Unlike C, there are multiple ways with which to free dynamically allocated memory, such as the operators deletedelete[](), and deallocation functions on allocator objects.

Do not call a deallocation function on anything other than nullptr, or a pointer returned by the corresponding allocation function described by:

AllocatorDeallocator
operator new()/newoperator delete()/delete
operator new[]()/new[]operator delete[]()/delete[]
placement operator new()N/A
allocator<T>::allocate()

allocator<T>::deallocate()

std::malloc(), std::calloc(),
std::realloc()
std::free()

...

In the first alternative (delete object), the value of the operand of delete may be a null pointer value, a pointer to a non-array object created by a previous new-expression, or a pointer to a subobject (1.8) representing a base class of such an object (Clause 10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete may be a null pointer value or a pointer value that resulted from a previous array new-expression. If not, the behavior is undefined.

Deallocating a pointer that is not allocated dynamically (including pointers obtained by calls to placement new()) is undefined behavior because the pointer value was not obtained by an allocation function. Deallocating a pointer that has already been passed to a deallocation function is undefined behavior because the pointer value no longer points to memory that has been dynamically allocated.

...

...