Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Add information about class-specific new/delete to the intro table; remove mention of a recommendation

...

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

AllocatorDeallocator
global operator new()/newglobal operator delete()/delete
global operator new[]()/new[]global operator delete[]()/delete[]
class-specific operator new()/newclass-specific operator delete()/delete
class-specific operator new[]()/new[]class-specific operator delete[]()/delete[]
placement operator new()N/A
allocator<T>::allocate()

allocator<T>::deallocate()

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

...

Code Block
bgColor#FFcccc
langcpp
#include <cstdlib>
void f() {
  int *i = static_cast<int *>(std::malloc(sizeof(int)));
  // ...
  delete i;
}

Additionally, this code violates MEM08-CPP. Use new and delete rather than raw memory allocation and deallocation. However, it This code does not violate MEM53-CPP. Explicitly construct and destruct objects when manually managing object lifetime because it complies with the MEM53-CPP-EX1 exception.

...