12.3 Free store

12.3.1 Correctly declare overloads for operator new and delete

operator new and operator delete should work together. Overloading operator new means that a custom memory management scheme is in operation for a particular class or program. If a corresponding operator delete (plain or array) is not provided the memory management scheme is incomplete. Additionally, if initialization of the allocated object fails with an exception, the C++ runtime will try to call an operator delete with identical parameters as the called operator new, except for the first parameter. If no such operator delete can be found, the memory will not be freed. If this operator delete does not actually need to perform any bookkeeping, one with an empty body should be defined to document this in the code. When declared in a class, operator new and operator delete are implicitly static members; explicitly including the static specifier in their declarations helps to document this.

#include <cstddef>
class C
static void* operator new (std::size_t size);
static void operator delete (void* ptr);   // @@+ Compliant +@@
void* operator new [] (std::size_t size);  // @@- Non-Compliant -@@


  • HIC++ v3.3 – 12.6
  • HIC++ v3.3 – 12.7

Click here for references