In a C++ game engine's memory-tracking build, how do you replace the global operator new and operator delete to count allocations, and what must the replacement cover?
answer
- define, do not declare in a namespace
- twenty replaceable signatures
- array, nothrow, aligned and sized forms
- never return null from the throwing form
- the compiler may omit calls
basics
~10 sDefine functions with the exact replaceable signatures at global scope, once, not inline. Cover the forms the code uses - scalar, array, sized, aligned - and never let the throwing operator new return null.
solid answer
~50 sThe global allocation and deallocation functions are *replaceable*: if the program defines one with the same signature at global scope - not `inline`, with C++ linkage, in exactly one translation unit - the linker uses it instead of the library's. C++23 lists twenty: `operator new`/`new[]` with optional `std::align_val_t` and `std::nothrow_t`, and `operator delete`/`delete[]` with optional size, alignment and nothrow. A counting build replaces at least `new`, `new[]`, `delete`, `delete[]` and their sized forms; the default nothrow forms call the replaced throwing ones, and the aligned forms matter for over-aligned types. The throwing form must return storage or throw `std::bad_alloc`, and must not call `new` recursively - use `malloc`. Tagging each block also catches `delete`/`delete[]` mismatches. Counts can drop at `-O2`, since compilers may omit replaceable allocation calls. The reserved placement form `operator new(std::size_t, void*)` cannot be replaced.
code
cpp · 41 lines#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <new>
namespace {
std::size_t g_live = 0, g_total = 0;
constexpr std::size_t kHeader = alignof(std::max_align_t); // keeps user memory aligned
enum Kind : unsigned char { Scalar = 1, Array = 2 };
void* track_alloc(std::size_t n, Kind k) {
auto* raw = static_cast<unsigned char*>(std::malloc(kHeader + n));
if (!raw) throw std::bad_alloc{}; // the throwing forms must never return null
raw[0] = k; ++g_live; ++g_total;
return raw + kHeader;
}
void track_free(void* p, Kind k) noexcept {
if (!p) return;
auto* raw = static_cast<unsigned char*>(p) - kHeader;
if (raw[0] != k) std::fprintf(stderr, "MISMATCH: %s on a block from %s\n",
k == Array ? "delete[]" : "delete", raw[0] == Array ? "new[]" : "new");
--g_live; std::free(raw);
}
} // namespace
void* operator new(std::size_t n) { return track_alloc(n, Scalar); }
void* operator new[](std::size_t n) { return track_alloc(n, Array); }
void operator delete(void* p) noexcept { track_free(p, Scalar); }
void operator delete[](void* p) noexcept { track_free(p, Array); }
void operator delete(void* p, std::size_t) noexcept { track_free(p, Scalar); }
void operator delete[](void* p, std::size_t) noexcept { track_free(p, Array); }
int main() {
int* hp = new int(100);
float* weights = new float[64];
char* maybe = new (std::nothrow) char[32]; // default nothrow form calls operator new[]
delete hp;
delete[] maybe;
delete weights; // BUG: undefined behaviour, should be delete[]
std::printf("total=%zu live=%zu\n", g_total, g_live);
}go deeper
Recall that you can define your own global operator new and operator delete to observe allocations, and that every new must still pair with a matching delete.
Explain the replacement rules - global scope, not inline, exact signatures, one definition - and why operator new must use malloc rather than new internally.
Cover the array, sized, aligned and nothrow forms deliberately, interpret counts that change with optimization, and use block tags to catch delete/delete[] mismatches.
Weigh whole-program global replacement against class-specific overloads and allocator injection, deciding which builds ship tracking and how its overhead is bounded.
## Replaceable functions The standard library supplies default definitions of the global allocation and deallocation functions and marks them **replaceable**. If your program provides a definition with the same signature, that **replacement function** is used everywhere instead - including by the standard library's own containers - with the replacement occurring before program startup. A replacement declaration: - must not be `inline`; - must be in the global namespace (attached to the global module) with C++ language linkage; - must match the declared return type and signature in `<new>`; - is subject to the one-definition rule, so define it in exactly one translation unit. A non-member allocation function must belong to the global scope and must not have internal linkage, so Clang rejects one inside a namespace ("'operator new' cannot be declared inside a namespace") or marked `static`. The `inline` rule needs no diagnostic; Clang still warns by default: "replacement function 'operator new' cannot be declared 'inline'" (`-Winline-new-delete`). ## The twenty signatures The C++23 draft lists twenty replaceable signatures, in four groups: | Group | Forms | |---|---| | `operator new` | `(size_t)`, `(size_t, align_val_t)`, `(size_t, const nothrow_t&)`, `(size_t, align_val_t, const nothrow_t&)` | | `operator delete` | `(void*)`, `(void*, size_t)`, `(void*, align_val_t)`, `(void*, size_t, align_val_t)`, `(void*, const nothrow_t&)`, `(void*, align_val_t, const nothrow_t&)` | | `operator new[]` | the four `operator new` forms with `[]` | | `operator delete[]` | the six `operator delete` forms with `[]` | You do not need to write all twenty, because the **default** versions chain to the others: 1. The default nothrow `operator new` calls the (possibly replaced) throwing `operator new` and returns null if it throws; a run here showed `new (std::nothrow) char[32]` going through a replaced `operator new[]`. 2. The default sized `operator delete(void*, std::size_t)` forwards to the unsized one. 3. If you define a **sized** delete, the standard says you **shall** also define the unsized one; if you define an unsized one, you **should** also define the sized one. 4. The aligned (`std::align_val_t`, C++17) forms are separate: types with new-extended alignment go through them, so a tracker that ignores them misses those allocations - and must not see their pointers in its unaligned delete. ## Rules for the replacement's body - **Never return null from the throwing form.** Its requirement - non-null storage or `std::bad_alloc` - binds replacement versions too. - **Do not allocate with `new` inside `operator new`.** That recurses. Use `std::malloc`, an OS call or a preallocated arena. - **Keep `operator delete` `noexcept`** and tolerant of a null pointer. - **Respect alignment.** Memory returned by the plain form must be suitably aligned for any object without new-extended alignment; a tracking header in front of the block must be a multiple of `alignof(std::max_align_t)`. - **Be careful with I/O.** Logging from inside `operator new` with something that itself allocates - an `std::string`, an iostream - can recurse; count first, report later. ## Sized deallocation Since C++14, a delete-expression may call the **sized** `operator delete(void*, std::size_t)` when the size is known, letting an allocator skip a size lookup. Whether the compiler emits sized calls is a toolchain setting: Apple clang 21 on this machine passes `-fno-sized-deallocation` to the compiler by default (visible with `clang++ -###`), so a replaced sized delete was called only after building with `-fsized-deallocation`. Define both forms so the tracker works either way. ## What the counts mean - The standard allows an implementation to **omit calls to replaceable global allocation functions**. Clang at `-O2` removed a non-escaping `new int(42)` / `delete` pair entirely: the same program reported three allocations at `-O0` and one at `-O2`. Count in the build you care about. - Allocations made before `main` and by the runtime itself are included, so establish a baseline. - Class-specific `operator new` overloads - a static member function in a class - bypass the global replacement for that class unless they forward to `::operator new`. Over-replacing global functions is a whole-program decision; a class-specific overload is local. ## Catching mismatches Storing a one-byte tag in the header - scalar or array - lets `operator delete` detect a block that came from `operator new[]`. The example flags `delete weights;` on a `new float[64]` block. That program still has undefined behaviour; the tracker is a debugging aid, and with a type that needs an array cookie the wrong address would reach the tracker instead. The reserved placement forms, `operator new(std::size_t, void*)` and its array and delete counterparts, cannot be replaced, so placement construction never appears in these counts.
- How does a class-specific operator new differ from replacing the global one?A class-specific `operator new` is a static member function found by class-scope lookup for `new Particle`, so it affects only that class and its derived classes, and it may coexist with any number of others. Replacing the global function affects every allocation in the program, including the standard library's. Pools for small hot types use class-specific overloads; tracking and debugging builds replace the globals. `::new` bypasses the class version.
- Why might a replaced operator new never be called even though it is defined?Common causes: its signature differs from the declared one, so an extra parameter makes it a placement overload rather than a replacement; the allocation uses a form you did not replace, such as the aligned overload for an over-aligned type; a class-specific `operator new` intercepts it; or the compiler omitted the call, which the standard permits for replaceable global allocation functions.
- Replacing operator new(std::size_t) alone captures every allocation, including over-aligned types
- A replacement may live in your own namespace
- Returning nullptr from the throwing operator new is a valid failure signal
- Allocation counts are identical at every optimization level
- You can replace placement new to track objects constructed in buffers