Rule A18-5-9 (required, implementation, automated)

Custom implementations of dynamic memory allocation and deallocation functions shall meet the semantic requirements specified in the corresponding “Required behaviour” clause from the C++ Standard.

Rationale

It is possible to provide custom implementations of global dynamic memory allocation/deallocation functions. Requirements for custom implementations for each function declaration are specified in the C++ Standard in the section “Required behaviour”. If the provided function do not implement the required semantics, it can lead to undefined behaviour.

Example

//% $Id: A18-5-9.cpp 305629 2018-01-29 13:29:25Z piotr.serwa $
#include <new>

void* operator new(std::size_t count, const std::nothrow_t& tag)

{
extern void* custom_alloc(std::size_t); // Implemented elsewhere; may return

nullptr
if (void *ret = custom_alloc(count))

{

return ret;
}
throw std::bad_alloc(); //non-compliant, this version of new method shall not throw exceptions

}

See also

SEI CERT C++ Coding Standard [10]: MEM55-CPP: Honor replacement dynamic storage management requirements