Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Aligned allocation overload matching

Learn how C++23 selects class-specific `operator new` and `operator delete` overloads and why the paired global calls must receive the same alignment value.

Version: ISO C++23; implementation support may vary

Syntax

// Class members for single-object allocation and deallocation:
static void* operator new(std::size_t bytes, std::align_val_t alignment);
static void operator delete(void* p, std::align_val_t alignment) noexcept;

// Expressions that manage the lifetime of a T object:
T* p = new T{};
delete p;

// Direct calls that manage raw storage:
void* raw = ::operator new(bytes, alignment);
::operator delete(raw, alignment);

new T{} obtains storage and initializes a T object in it. In the case shown here, delete p destroys the object and then releases its storage. Direct calls to operator new and operator delete manage raw storage: they do not call T's constructor or destructor.

In C++23, aligned allocation overload matching starts with name lookup. new T{} looks for operator new in the class first. Declarations found there hide global overloads; ::new T{} skips class scope. If alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__, the first overload resolution attempt passes alignof(T) as a std::align_val_t after the size; if no function matches, it tries without the alignment. For other types, the order is reversed. A class overload without an alignment parameter must still return properly aligned storage.

For the type shown here, ordinary delete p looks for operator delete in the class and looks globally if it finds no declaration there. It selects a deallocation function independently of the allocation function: above the same threshold it prefers a form with std::align_val_t; otherwise it prefers a form without it. If construction throws after allocation succeeds, the new expression calls a suitable deallocation function if one is found, then the exception continues to propagate.

Pass the pointer returned by ::operator new(bytes, alignment) to ::operator delete(p, alignment) with exactly the same alignment value. Using the unaligned overload or a different alignment value is undefined behavior. ::delete p is still an expression that destroys the object, but it bypasses class-specific deallocation, so it is not a substitute for delete p in this example.

This example covers one object; arrays pair new[] with delete[]. outstanding counts successful allocations through this class that have not yet been released, not bytes or live objects. A return to zero does not establish memory safety.

Examples

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

struct alignas(64) Block {
    inline static std::size_t outstanding = 0;
    int value{};

    static void* operator new(std::size_t bytes, std::align_val_t alignment) {
        void* p = ::operator new(bytes, alignment);
        ++outstanding;
        return p;
    }

    static void operator delete(void* p, std::align_val_t alignment) noexcept {
        if (p == nullptr) return;
        ::operator delete(p, alignment);
        --outstanding;
    }
};

static_assert(alignof(Block) > __STDCPP_DEFAULT_NEW_ALIGNMENT__);

int main() {
    std::cout << Block::outstanding << '\n';
    Block* p = new Block{};
    std::cout << Block::outstanding << '\n';
    delete p;
    std::cout << Block::outstanding << '\n';
}

Result

0
1
0

With successful allocation, the expected lines are `0`, `1`, `0`. The target must support `alignas(64)` and satisfy the `static_assert`; support varies by implementation.

Related lessons

  • C++ · Lesson 20 · Advanced · Version: 3

Sources