Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Memory: ownership, copies, and object lifetime

Separate storage from the object living in it and from responsibility for a resource. Understand automatic and dynamic storage duration, copy semantics, placement new, explicit destruction, and strict-aliasing restrictions.

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

Syntax

T copy = original; T* alias = &original; T* p = ::new (static_cast<void*>(storage)) T{args}; p->~T();

Storage is space for data; lifetime concerns an object occupying that space. It generally begins once suitably sized and aligned storage is obtained and initialization finishes. For a class object, it ends when the destructor call starts; releasing or reusing storage can also end it. A valid address alone does not permit access to an object.

Storage duration determines storage availability. An ordinary local variable has automatic storage duration: its storage lasts until its block exits. Objects created by allocating new have dynamic storage duration and can outlive the block. A local std::unique_ptr can have automatic storage duration while its managed object has dynamic storage duration. “Stack” and “heap” are typical implementation models, not definitions of these concepts.

Ownership means responsibility for releasing a resource. Use RAII: the owner's destructor performs cleanup. std::unique_ptr is movable but not copyable. Copying a raw pointer copies its address, not its object or responsibility for deleting it. Default class copying copies members; a pointer member still points to the same object. Consequently, a copy need not be deep, and two independent owners of the same address risk double deletion.

Placement new. In ::new (storage) T{args}, the leading :: restricts allocation-function lookup to global scope, but does not force selection of the standard void* overload. A global overload that better matches the type of storage can allocate memory. The example uses the form ::new (static_cast<void*>(storage)) T{args} with Item, selecting the standard non-allocating overload and constructing the object in the supplied storage. The cast to void* alone does not guarantee that selection for arbitrary T. If T has new-extended alignment (alignof(T) > __STDCPP_DEFAULT_NEW_ALIGNMENT__), the first pass of overload resolution includes a std::align_val_t argument before the placement argument. A viable global operator new(std::size_t, std::align_val_t, void*) can then be selected instead of the standard non-allocating overload. The storage must be large enough and correctly aligned. p->~T() explicitly destroys the object but does not release storage. Do not use delete for an object placed in a local buffer. Do not destroy an object twice; after explicitly destroying an ordinary local variable of a type with a non-trivial destructor, correctly reconstruct an object of the original type before automatic destruction occurs.

Strict aliasing restricts the types through which an object's value may be read or modified. reinterpret_cast does not legalize access through an incompatible type. Representation access through char, unsigned char, or std::byte is permitted, but does not replace lifetime rules. The example uses a C++23-compatible subset; the supplied standard URL is a living draft, not a frozen C++23 edition.

Reference identifiers covered by this entry: Object lifetime, placement new, explicit destruction and strict aliasing, Automatic and dynamic storage duration.

Examples

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

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

int main() {
    Item original{7};
    Item copy = original;
    Item* alias = &original;
    alias->value = 9;

    auto owner = std::make_unique<Item>();
    owner->value = 11;

    alignas(Item) std::byte storage[sizeof(Item)];
    Item* placed = ::new (static_cast<void*>(storage)) Item{13};

    std::cout << original.value << ' ' << copy.value << ' '
              << owner->value << ' ' << placed->value << '\n';

    placed->~Item();
}

Result

9 7 11 13

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