Replace new and delete

The C++ standard provides eight signatures, which you can replace with functions using the Threading Building Blocks scalable memory allocator. There are four pairs of new/delete operators, which provide throw/no-throw versions of each as well as scalar and array forms. Example 6-1 shows the complete list of signatures.

Example 6-1. ISO 14882 C++ new/delete

void* operator new(std::size_t size) throw(std::bad_alloc);
void* operator new(std::size_t size, const std::nothrow_t&) throw();
void* operator new[](std::size_t size) throw(std::bad_alloc);
void* operator new[](std::size_t size, const std::nothrow_t&) throw();
void  operator delete(void* ptr) throw();
void  operator delete(void* ptr, const std::nothrow_t&) throw();
void  operator delete[](void* ptr) throw();
void  operator delete[](void* ptr, const std::nothrow_t&) throw();

Replacing all eight signatures is the only way to ensure portability. Some implementations may simply implement the array forms in terms of the scalar forms, but relying on that could lead to more problems than it is worth if that assumption proves not to be true in the future.

The replacements you write for new and delete have to go in the right place (before any use of new or delete).

Actual code to do new/delete replacement can be found in Chapter 11 in Example 11-50 (“Replacement of new and delete functions, demonstration”).

Warning

Consult your C++ compiler documentation carefully to understand limitations and other issues. Understanding ...

Get Intel Threading Building Blocks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.