# Object lifetime and storage

> Why raw memory allocation differs from object existence: storage durations, [basic.life] rules, implicit-lifetime types, std::launder, and temporary lifetime extension.

- **Source:** C++ Atlas (https://cppatlas.com/topics/object-lifetime-and-storage)
- **Track:** Memory model
- **Estimated reading time:** 11 minutes
- **ISO C++ clauses:** [basic.stc] (https://eel.is/c++draft/basic.stc), [basic.life] (https://eel.is/c++draft/basic.life), [intro.object] (https://eel.is/c++draft/intro.object), [ptr.launder] (https://eel.is/c++draft/ptr.launder), [class.temporary] (https://eel.is/c++draft/class.temporary), [stmt.ranged] (https://eel.is/c++draft/stmt.ranged)
- **cppreference links:** [Lifetime](https://en.cppreference.com/w/cpp/language/lifetime), [Storage class specifiers](https://en.cppreference.com/w/cpp/language/storage_duration), [Object](https://en.cppreference.com/w/cpp/language/object), [std::launder](https://en.cppreference.com/w/cpp/utility/launder), [std::start_lifetime_as](https://en.cppreference.com/w/cpp/memory/start_lifetime_as), [std::is_implicit_lifetime](https://en.cppreference.com/w/cpp/types/is_implicit_lifetime), [Range-based for loop](https://en.cppreference.com/w/cpp/language/range-for)
- **Prerequisites:** initialization-forms, value-categories, raii-and-resource-management

C++ separates **where and how long raw bytes exist** from **when a typed object legally lives inside those bytes**. Conflating the two leads to dangling pointers, broken placement-new code, and optimizer surprises when compilers reason about `const` members or uninitialized buffers.

## Storage duration versus object lifetime

Every region of memory backing an object has one of four **storage durations** (`[basic.stc]`):

| Storage duration | Obtained and released | Typical examples | Thread sharing |
| --- | --- | --- | --- |
| Static | Program startup until exit | Globals, `static` locals | Shared across all threads |
| Thread | Thread launch until thread exit | `thread_local` variables | Private per thread |
| Automatic | Block entry until block exit | Local variables, parameters | Stack-bound unless address shared |
| Dynamic | Explicit `new`/`malloc` until `delete`/`free` | Heap allocations, containers | Governed by ownership |

Storage duration governs only the raw memory. Under `[basic.life]`, an object's **lifetime** is a runtime property that starts only after storage with proper size and alignment is obtained **and** initialization completes (or vacuous initialization finishes for trivial types). Lifetime ends either when the destructor call **starts** (for class types with non-trivial destructors) or when the storage is reused or released.

> **Mental model.** Storage duration is a lease on an apartment; object lifetime is the tenancy itself. Holding keys to an empty apartment does not mean a tenant lives inside.

> **Standardese.** Per `[basic.life]/1`, for any class type with a non-trivial destructor, object lifetime ends the instant execution enters its destructor—before the destructor body even runs its first statement.

## Outside lifetime: what you can and cannot touch

Suppose you allocate raw aligned storage on the stack (`alignas(T) std::byte buf[sizeof(T)]`) or obtain memory from `operator new`. Before an object's lifetime begins—or after its destructor runs while the storage still remains allocated—a pointer `T*` pointing to that address has severe restrictions (`[basic.life]/6–7`):

- **Allowed:** converting the pointer to `void*`, passing it to placement `new` or `std::construct_at`, or comparing addresses (while storage still exists).
- **Undefined behavior:** reading or writing non-static data members, invoking non-static member functions, casting to a base class, or calling `delete` on the typed pointer.

C++20 added `std::construct_at(ptr, args...)` and `std::destroy_at(ptr)` in `<memory>` so custom containers and pools can start and end lifetimes cleanly—even inside `constexpr` evaluation, where raw placement `new` was historically disallowed until C++26.

## Implicit-lifetime types and start_lifetime_as

Before C++20, calling `std::malloc(sizeof(Packet))` and casting the returned `void*` to `Packet*` technically created undefined behavior because `malloc` allocates bytes without running constructors. Real C and systems code relied on it anyway.

C++20 solved this cleanly in `[intro.object]` by defining **implicit-lifetime types**: scalar types, aggregate classes with no user-provided constructors and trivial destructors (or eligible implicit-lifetime class types without user-provided destructors), and arrays of those types. Certain operations—`std::malloc`, `std::calloc`, `std::realloc`, `std::memcpy`, `std::memmove`, `operator new`, and creating a `std::byte` or `unsigned char` array—**implicitly create objects** of suitable implicit-lifetime types inside the region so the program's subsequent access has defined behavior.

C++23 went one step further with `std::start_lifetime_as<T>(ptr)` (and `std::start_lifetime_as_array<T>(ptr, n)`) for situations where raw bytes already hold a valid representation—such as memory-mapped files or socket buffers—and you want to read them in-place without copying:

```cpp
#include <cstddef>
#include <memory>
#include <type_traits>

struct PacketHeader {
    unsigned int seq;
    unsigned int length;
};
static_assert(std::is_implicit_lifetime_v<PacketHeader>);

unsigned int inspect_wire(const std::byte* aligned_bytes) {
    // Begins lifetime of PacketHeader in-place without copying or overwriting bytes.
    const auto* hdr = std::start_lifetime_as<PacketHeader>(aligned_bytes);
    return hdr->seq + hdr->length;
}
```

Ensure `aligned_bytes` already satisfies `alignof(PacketHeader)` and covers `sizeof(PacketHeader)` valid bytes; `std::start_lifetime_as` performs zero runtime instructions and zero-copy type blessing for the abstract machine.

## Reusing storage and why std::launder exists

When you destroy an object in place and placement-new a brand-new object into the **exact same address**, can older pointers `p` to the original object keep working?

Under `[basic.life]/8`, an existing pointer or reference is **transparently replaceable** only if every one of these holds:

1. The new object occupies the exact same storage location and has the **exact same type** (ignoring top-level cv-qualifiers).
2. The old object was a complete object (not a base-class subobject) or an array element whose replacement is also an array element.
3. The type contains **no `const` non-static data members** and **no reference non-static data members**.

If your struct contains a `const` field or reference member, an optimizer seeing `p->id` earlier may legally cache that constant forever—even across a placement `new (p)` call! You must either use the pointer returned by placement `new` directly or pass the stale pointer through `std::launder(p)` (`[ptr.launder]`):

```cpp
#include <memory>
#include <new>

struct Node {
    const int id;
    int payload;
};

int reuse_const_member() {
    Node* p = new Node{1, 10};
    p->~Node();

    Node* q = ::new (p) Node{2, 20};
    // p->id would be UB here because Node has a const member!
    int from_new = q->id;               // Well-defined: 2
    int from_launder = std::launder(p)->id; // Well-defined: 2

    std::destroy_at(q);
    ::operator delete(q);
    return from_new + from_launder;
}
```

> **Pitfall.** Placement-new reusing storage that holds a `const` or reference non-static member does **not** update pre-existing pointers to that address; dereferencing the old pointer without `std::launder` lets the compiler reuse stale cached values under `[basic.life]/8`.

Notice what `std::launder` does **not** do: it never creates an object or bypasses strict aliasing. It purely serves as an optimization barrier declaring that an already-live object of type `T` now resides at that address.

## Temporary materialization and the range-for fix

Temporaries materialized from prvalues normally survive only until the end of the **full-expression** containing them (typically the semicolon). Direct binding to a `const T&` or `T&&` reference extends the temporary's lifetime to match the reference's scope (`[class.temporary]`).

That rule has a notorious boundary: **lifetime extension never propagates through function calls or chained member accesses** (except direct subobject selection on the temporary itself, like `const int& m = make_pair().first;`). Calling `const char* s = make_name().c_str();` or capturing a `std::string_view` over a temporary `std::string` creates an instant dangling pointer at the semicolon.

For twelve years, range-based `for` loops suffered from the exact same cliff because their desugaring bound `auto&& __range = range_expr;`. Writing `for (int v : make_holder().items)` extended only `.items` while destroying the outer `Holder` temporary before iteration began! C++23 paper P2718R0 finally fixed `[stmt.ranged]`:

```cpp
#include <vector>

struct Holder {
    std::vector<int> items{1, 2, 3};
};
Holder make_holder() { return {}; }

int sum_items() {
    int total = 0;
    // Pre-C++23: UB (Holder temporary died before loop body).
    // C++23 (P2718R0): Well-defined; all temporaries in initializer extend across loop.
    for (int v : make_holder().items) {
        total += v;
    }
    return total;
}
```

> **Pitfall.** Even in C++23, temporary lifetime extension protects only direct reference binding and range-for initializers—returning a view (`std::string_view`, `std::span`) or calling `.c_str()` on a temporary still dangles the moment the full-expression ends.

## What to remember

- **Storage duration** controls raw memory allocation; **object lifetime** starts only after initialization completes and ends when destruction begins or storage is reused.
- Never read members or call member functions on uninitialized or already-destroyed storage; use `std::construct_at` and `std::destroy_at` to manage raw buffers cleanly.
- Use C++20 **implicit-lifetime types** and C++23 **`std::start_lifetime_as`** instead of pointer punning when interpreting raw byte buffers in place.
- Capture placement `new`'s return pointer or apply **`std::launder`** whenever replacing an object whose class has `const` or reference members.
- Remember that C++23 extended temporary lifetimes inside **range-for initializers** (`P2718R0`), while view types and accessors on temporaries outside range-for still dangle at the semicolon.

## Concept Map Outline

- **Object Lifetime and Storage:** Distinguishing when raw memory exists from when a C++ object legally lives inside it.
  - **Four Storage Durations:** Rules governing allocation and release of raw memory backing objects.
    - **Static and Thread:** Program-wide shared globals and per-thread thread_local storage lifetimes.
    - **Automatic and Dynamic:** Scope-bound stack storage versus explicit heap allocations via new or malloc.
  - **Lifetime Lifecycle Rules:** Exact boundaries when an object starts and stops existing.
    - **Start Upon Initialization:** Lifetime begins only once aligned storage is obtained and initialization completes.
    - **End When Destructor Starts:** Non-trivial objects lose lifetime the moment their destructor starts executing.
    - **construct_at and destroy_at:** C++20 utilities starting and ending lifetimes explicitly even inside constexpr.
  - **Implicit-Lifetime Types:** C++20 and C++23 rules blessing raw byte buffers without constructors.
    - **Implicit Object Creation:** malloc, memcpy, and byte arrays implicitly create scalars and eligible aggregates.
    - **std::start_lifetime_as:** C++23 zero-copy helper beginning object lifetime over existing wire bytes.
  - **Storage Reuse and Launder:** Pointer validity rules when placement new replaces an existing object.
    - **Transparent Replaceability:** Old pointers stay valid only for exact same types lacking const or reference members.
    - **std::launder Barrier:** Prevents optimizers from caching stale const or reference values across placement new.
  - **Temporary Lifetime Extension:** When materialized prvalues outlive their enclosing full-expression.
    - **Direct Reference Binding:** Binding const T& or T&& directly extends a temporary to the reference scope.
    - **Accessor Dangling Cliff:** Calling .c_str() or returning views stops extension at the semicolon.
    - **C++23 Range-For Fix:** P2718R0 extends all temporaries inside the range-for initializer across the loop.

## Standardese Quiz Questions

### Question 1 [warmup] ([basic.life])

When does the lifetime of an object with a non-trivial destructor end according to [basic.life]?

A. Only after free() or operator delete returns the storage to the OS
B. Once all pointers to the object go out of scope
C. The moment the destructor call starts, before its body finishes
D. When the last statement inside the destructor body finishes executing

**Answer (C):** The moment the destructor call starts, before its body finishes

**Why:** Under [basic.life]/1, for an object of a class type with a non-trivial destructor, lifetime ends when the destructor call starts (or when storage is reused/released without running the destructor).

### Question 2 [warmup] ([obj.lifetime], [intro.object])

Which C++23 standard library function starts the lifetime of an implicit-lifetime type T in existing raw bytes without copying or overwriting their contents?

A. std::start_lifetime_as<T>
B. std::launder<T>
C. std::construct_at<T>
D. std::assume_aligned<T>

**Answer (A):** std::start_lifetime_as<T>

**Why:** C++23 std::start_lifetime_as<T>(ptr) implicitly creates an object of implicit-lifetime type T whose value corresponds to the existing byte representation at ptr, avoiding a redundant std::memcpy.

### Question 3 [tricky] ([stmt.ranged], [class.temporary])

In C++17 and C++20 (before C++23 P2718R0), what happens in this range-based for loop?

```cpp
struct Holder { std::vector<int> items{1, 2, 3}; };
Holder make_holder();
for (int x : make_holder().items) {
    use(x);
}
```

A. Well-defined because range-for always extends the entire initializer expression
B. Ill-formed because make_holder() returns a prvalue
C. Well-defined only if int x is replaced with auto&& x
D. Undefined behavior because the Holder temporary is destroyed before the loop body begins

**Answer (D):** Undefined behavior because the Holder temporary is destroyed before the loop body begins

**Why:** Before C++23, range-for desugared to auto&& __range = make_holder().items;. Because __range bound to the member reference returned/accessed rather than extending Holder across the whole expression, Holder died at the semicolon before iteration started. C++23 P2718R0 fixed this specifically for range-for.

### Question 4 [tricky] ([class.temporary])

Why does std::string_view sv = std::string("hello") + " world"; produce a dangling view despite const reference extension rules?

```cpp
std::string_view sv = std::string("hello") + " world";
```

A. std::string_view allocates on the heap and leaks
B. Temporary lifetime extension applies to direct reference bindings, not user-defined class values like std::string_view
C. std::string literals are destroyed at block entry
D. String concatenation yields an lvalue rather than a prvalue

**Answer (B):** Temporary lifetime extension applies to direct reference bindings, not user-defined class values like std::string_view

**Why:** Lifetime extension under [class.temporary] extends a temporary ONLY when bound directly to a reference (const T& or T&&). std::string_view is a class type holding a pointer and length; the temporary std::string dies at the semicolon, leaving sv dangling.

### Question 5 [standardese] ([basic.life], [ptr.launder])

After destroying a Node with a const int member and constructing a new Node in the same storage via ::new (p) Node{2}, why is reading p->id directly undefined behavior?

```cpp
struct Node { const int id; };
Node* p = new Node{1};
p->~Node();
::new (p) Node{2};
```

A. Because Node has a const non-static data member, old pointer p is not transparently replaceable under [basic.life]/8
B. Because placement new changes the virtual address of p
C. Because const int members cannot be destroyed explicitly
D. Because Node lacks a user-declared move constructor

**Answer (A):** Because Node has a const non-static data member, old pointer p is not transparently replaceable under [basic.life]/8

**Why:** Under [basic.life]/8, an old pointer automatically refers to a newly created object at the same storage ONLY if the class has no const or reference non-static data members (among other rules). You must use the pointer returned by placement new or std::launder(p).

### Question 6 [standardese] ([ptr.launder])

Which of the following is true of std::launder(p) when p points to raw bytes where no object of type T has ever been constructed?

A. std::launder(p) implicitly begins the lifetime of a T at p
B. std::launder(p) disables strict aliasing rules for T
C. Calling std::launder(p) violates its precondition because no live object of type T exists at that address
D. std::launder(p) converts any void* into a valid T* safely

**Answer (C):** Calling std::launder(p) violates its precondition because no live object of type T exists at that address

**Why:** Per [ptr.launder], std::launder(p) requires that an object of type T is ALREADY within its lifetime at the address represented by p. It never creates an object (use placement new, std::construct_at, or std::start_lifetime_as for that).
