Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Automatic storage duration versus dynamic allocation

Distinguish an object’s storage duration and lifetime from any separate allocations made by its members, such as storage for a `std::string`’s characters.

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

Syntax

T local{args...};                                      // Direct declaration; automatic in an ordinary block.
std::unique_ptr<T> owner = std::make_unique<T>(args...); // The local owner and its dynamically created T are separate objects.
owner.reset();                                         // Destroy the owned T; owner remains alive and becomes empty.

Start with automatic storage duration versus dynamic allocation: storage duration describes how long storage can remain available for an object; lifetime describes when the object itself exists. An ordinary block variable declared without static, thread_local, or extern has automatic storage duration. A constructed class object is destroyed on block exit, including during exception unwinding. This is a language rule, not a requirement to place the object on a physical stack.

std::make_unique<Record>() creates a Record with a new expression and returns a std::unique_ptr<Record> that owns it exclusively. In the example, owner itself has automatic storage duration, but the Record it owns has dynamic storage duration and survives the inner block. owner.reset() destroys that Record and deallocates its storage; the now-empty owner stays alive. Do not use a pointer or reference to access a destroyed object.

The Record::text member has the same storage duration as its containing Record. Storage for the characters is a separate matter: std::string may obtain it through its allocator as needed. Small-string optimization (SSO) may store characters inside the string object, but C++23 requires neither SSO nor a particular length cutoff.

“Stack versus heap” is informal shorthand, not a physical placement guarantee. Prefer a direct local object when its lifetime naturally ends with the block, and std::unique_ptr when a separate, dynamically created object needs an exclusive owner.

Examples

#include <iostream>
#include <memory>
#include <string>

struct Record {
    std::string text;
};

int main() {
    std::unique_ptr<Record> owner;
    {
        Record local{"abc"};
        owner = std::make_unique<Record>();
        owner->text.assign(1000, 'x');
        std::cout << local.text.size() << ' ' << owner->text.size() << '\n';
    }
    std::cout << owner->text.size() << '\n';
    owner.reset();
    std::cout << (owner ? "owns a record" : "empty") << '\n';
}

Result

3 1000
1000
empty

If allocation and output succeed, the program prints both string lengths, then the owned string’s length again after the inner block, and finally `empty` after `reset()`. The output does not show addresses or count allocations.

Related lessons

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

Sources