Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Class-specific allocation and deallocation operators

A class can define its own operator new and operator delete to control storage allocation and release. Overloads taking std::align_val_t handle alignment and must cooperate with the appropriate storage-release function.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

static void* operator new(std::size_t n);
static void operator delete(void* p) noexcept;
static void* operator new(std::size_t n, std::align_val_t a);
static void operator delete(void* p, std::align_val_t a) noexcept;

Class-specific operator new and operator delete manage storage; they do not replace constructors or destructors. They are static even without static and cannot be virtual. Ordinary new T first looks for the allocation function in the class; declarations found there can hide global overloads. ::new T forces global lookup, while ::delete p bypasses class-specific deallocation functions. Do not bypass them if doing so would break allocation/deallocation compatibility.

For alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__, the first attempt to select operator new includes a std::align_val_t argument. If no match exists, selection is retried without it; for other types, the order is reversed. Merely having a class-specific operator new(std::size_t) therefore does not guarantee stronger alignment: its implementation must provide storage suitable for the object being created.

Provide compatible release paths: release storage obtained through global ::operator new(n, a) through ::operator delete(p, a) with the same alignment. For ordinary delete, preference for an overload taking std::align_val_t depends on the type's alignment. In class scope, after applying that preference, the overload without std::size_t wins over one taking a size. Deallocation should not throw exceptions.

operator new[] and operator delete[] are a separate pair: single-object overloads do not intercept arrays. If initialization fails, the new expression can invoke an appropriate deallocation function. The example covers only a single object; both allocation paths delegate to the global aligned overload, and both release paths delegate to its counterpart.

Reference identifiers covered by this entry: Class-specific operator new/delete and matching aligned overloads.

Examples

#include <cstddef>
#include <iostream>
#include <new>

// This declaration requires implementation support for 64-byte alignment.
// C++23 does not guarantee that support.
struct alignas(64) Node final {
    int value = 42;
    inline static int allocations = 0;
    inline static int deallocations = 0;

    static void* operator new(std::size_t n) {
        return Node::operator new(n, std::align_val_t{alignof(Node)});
    }

    static void* operator new(std::size_t n, std::align_val_t a) {
        void* p = ::operator new(n, a);
        ++allocations;
        return p;
    }

    static void operator delete(void* p) noexcept {
        Node::operator delete(p, std::align_val_t{alignof(Node)});
    }

    static void operator delete(void* p, std::align_val_t a) noexcept {
        ++deallocations;
        ::operator delete(p, a);
    }
};

int main() {
    Node* p = new Node;
    const int value = p->value;
    delete p;
    std::cout << value << ' ' << Node::allocations << ' '
              << Node::deallocations << '\n';
}

Result

42 1 1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources