Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Object pools, arenas and stale pointer reuse

Pools and arenas manage storage, but an address does not establish object identity. Reusing a slot can leave a pointer without a live target or make it refer to a different object. Distinguish storage duration, object lifetime, and application-level handle validity.

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

Syntax

alignas(T) unsigned char storage[sizeof(T)]; T* p = ::new (static_cast<void*>(storage)) T{args}; p->~T();

Pool versus arena

An object pool typically reuses individual slots; an arena groups allocations and reclaims storage collectively. std::pmr::unsynchronized_pool_resource manages memory blocks, not application objects. std::pmr::monotonic_buffer_resource represents an arena strategy: individual deallocation does not reclaim space. A memory resource itself does not invoke the destructors of objects stored in its memory.

Storage versus object

The illustrated Item needs sufficient size, proper alignment, and completed initialization. placement new constructs an object at the supplied location; calling the destructor ends its lifetime without releasing the slot. Reading stale->value between destruction and reconstruction has undefined behavior.

Same address, different identity

In the example, a new complete Item exactly replaces the previous non-const complete object of the same type. Transparent replacement applies: stale automatically refers to the new object and reading it yields 20. This is language-valid but may violate the application's expectation that a handle still identifies the original item. Do not generalize this to arbitrary types or subobjects. std::launder has preconditions; it neither revives a destroyed object nor validates logical identity.

Implementation rules

Use the pointer returned by construction. If old handles must be rejected, check slot occupancy and a generation, with a policy preventing collisions after counter wraparound. Handle constructor exceptions, destroy objects requiring cleanup before resetting an arena, and synchronize concurrent access. The example demonstrates one slot, not a complete pool. The compilation target is C++23; a production runtime is not attested. The supplied sources are an evolving working draft, not a frozen C++23 edition.

Reference identifiers covered by this entry: Object pools, arenas and stale pointer reuse.

Examples

#include <iostream>
#include <new>

struct Item {
    int value;
    ~Item() noexcept {}
};

int main() {
    alignas(Item) unsigned char slot[sizeof(Item)];
    Item* first = ::new (static_cast<void*>(slot)) Item{10};
    Item* stale = first;
    std::cout << first->value << '\n';

    first->~Item();
    // Do not access stale->value here: no live Item exists.

    Item* current = ::new (static_cast<void*>(slot)) Item{20};
    std::cout << current->value << '\n';
    // Transparent replacement: stale now denotes the new Item.
    std::cout << stale->value << '\n';

    current->~Item();
}

Result

10
20
20

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