Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Object lifetime and strict aliasing

Storage can outlast the object it holds. Lifetime determines when an object exists, while strict aliasing determines which expression types may access its stored value.

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

Syntax

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

// Schematic: T is a class type; args... denotes its initializer arguments.
// Headers: <cstddef> for std::byte; <new> for placement new and std::launder.
// When its preconditions are met: T* q = std::launder(candidate);

In ISO C++23, object lifetime and strict aliasing answer different questions: is there a live object here, and may this expression access its value? Normally, lifetime begins once suitably sized and aligned storage is available and initialization is complete. A class object's lifetime ends when its destructor call starts; it can also end if its storage is released or reused by an object not nested within it. Construction and destruction have special rules.

::new (static_cast<void*>(storage)) T{args...} selects the standard global non-allocating placement form. The allocation function operator new returns the supplied address, and the new-expression constructs T there and returns a T*. p->~T() calls the destructor without releasing the arena or buffer storage. Do not use delete p for an object constructed in such a buffer or destroy it twice.

After destruction, the storage may still be available, but do not read or write the object outside its lifetime. When constructing a replacement, use the pointer returned by the new-expression. With transparent replacement, old pointers, references and the object's name automatically refer to the replacement once it is live. One example is a non-const complete object replaced by a complete object of the same type in exactly the same storage.

std::launder(p) can obtain a pointer to the replacement when old handles do not automatically refer to it. A live object of a similar type, in the standard's sense, must already be at that address, and every byte reachable through the result must be reachable through p. std::launder does not start a lifetime, fix alignment or bypass aliasing rules.

Strict aliasing concerns the type of the glvalue, the expression that denotes the object. Accessing the stored value through a type that is not permitted is undefined behavior. The permitted types are those similar to the object's dynamic type, its corresponding signed or unsigned type, and char, unsigned char or std::byte for access to its object representation. signed char is not a general byte-access exception. A reinterpret_cast by itself neither legitimizes access nor creates an object.

Examples

#include <cstddef>
#include <cstdio>
#include <new>

struct Item {
    int value;
    ~Item() { std::puts("destroy"); }
};

int main() {
    alignas(Item) std::byte storage[sizeof(Item)];
    void* slot = storage;

    Item* p = ::new (slot) Item{7};
    const unsigned int& alias = reinterpret_cast<const unsigned int&>(p->value);
    std::printf("%u\n", alias);
    p->~Item();

    Item* q = ::new (slot) Item{9};
    std::printf("%d\n", q->value);
    q->~Item();
}

Result

7
destroy
9
destroy

The program reads the live `int` member through a reference to the corresponding `unsigned int` type. After destroying the first `Item`, it constructs a second in the same buffer and uses the new pointer. Each destructor prints `destroy`; neither releases the buffer.

Related lessons

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

Sources