Skip to content
Tutorials Center
My learning

Topics

C++ Reference ยท Types and Language Concepts

Object lifetime and storage duration

Concept page on C++ object lifetime: automatic, static, thread and dynamic storage, construction and destruction order, temporaries and dangling references.

Every object in C++ has a lifetime: a span of time during which it exists and may be used. Lifetime begins when storage is obtained and the constructor finishes, and ends when the destructor starts (or, for objects with a trivial destructor, when the storage is released or reused). Using an object outside its lifetime is undefined behavior, which is why dangling pointers and references are among the most serious C++ bugs.

How long the storage lasts is its storage duration: automatic (local variables, released at the end of the block), static (globals and static variables, live for the whole program), thread (thread_local, one per thread) and dynamic (created with new, live until you delete them). Lifetime and storage duration are related but not identical: dynamic storage can hold objects that you construct and destroy several times.

C++
{
    std::string s = "hi";    // automatic: constructed here
}                            // destroyed here, in reverse order of construction

Try it

C++ Construction and destruction order

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

C++ Temporaries and lifetime extension

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.

Lifetime rules

Storage durations

Duration Created Destroyed
Automatic When execution reaches the declaration At the end of the enclosing block, in reverse order
Static Namespace-scope: before main (in order within a file, unspecified across files). Function-local static: first time control passes through After main returns, in reverse order of construction
Thread (thread_local) When the thread starts or on first use When the thread exits
Dynamic new (or an allocator) delete (or deallocate)

Subobjects

Base classes are constructed first (in declaration order), then members in declaration order, then the constructor body runs. Destruction is the exact reverse: body, members in reverse, then bases. The order of the member initializer list is irrelevant; the order of declarations in the class is what counts.

Temporaries

A temporary is destroyed at the end of the full expression that created it. Binding a temporary to a const T& or T&& local variable extends its lifetime to that reference's scope. Extension applies only to the reference initialized directly from the temporary: it does not carry through a function that returns a reference to its argument, and (before C++23) not to temporaries inside a range-based for range expression other than the outermost one.

Usage notes

  • Dangling reference: returning a reference or pointer to a local variable, or storing std::string_view/std::span over a temporary string, leaves the reference outliving its object. Compile with -Wall -Wextra and sanitizers to catch it.
  • Iterator invalidation is a lifetime issue: push_back on a std::vector may reallocate and end the lifetime of every element and every iterator, pointer or reference to them.
  • Static initialization order fiasco: the relative order of globals in different translation units is unspecified. Use function-local statics (initialized on first use, thread-safe since C++11) to avoid it.
  • Destructors must not throw out by default (they are implicitly noexcept). If they do, std::terminate is called.
  • Use RAII: tie resource lifetime to an object so that destruction releases it, as std::unique_ptr, std::lock_guard and containers do. Avoid raw new/delete; see new and delete.
  • Moved-from objects are still alive and must stay destructible; only their value is unspecified.
  • Reusing storage: placement new starts a new object's lifetime in existing storage; use std::launder when a pointer to the old object must reach the new one. A rare tool.
  • Exit paths: std::exit runs static destructors but not those of locals; std::abort runs none; std::quick_exit skips statics.