# Smart pointers, control blocks, and allocators

> How unique_ptr achieves zero-overhead ownership, what a shared_ptr control block stores, and how allocator_traits and std::pmr decouple allocation strategy from container types.

- **Source:** C++ Atlas (https://cppatlas.com/topics/smart-pointers-and-allocators)
- **Track:** Memory model
- **Estimated reading time:** 9 minutes
- **ISO C++ clauses:** [unique.ptr] (https://eel.is/c++draft/unique.ptr), [util.smartptr.shared] (https://eel.is/c++draft/util.smartptr.shared), [util.smartptr.weak] (https://eel.is/c++draft/util.smartptr.weak), [util.smartptr.enab] (https://eel.is/c++draft/util.smartptr.enab), [allocator.requirements] (https://eel.is/c++draft/allocator.requirements), [allocator.traits] (https://eel.is/c++draft/allocator.traits), [mem.res] (https://eel.is/c++draft/mem.res)
- **cppreference links:** [std::unique_ptr](https://en.cppreference.com/w/cpp/memory/unique_ptr), [std::make_unique](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique), [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr), [std::make_shared](https://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared), [std::weak_ptr](https://en.cppreference.com/w/cpp/memory/weak_ptr), [std::enable_shared_from_this](https://en.cppreference.com/w/cpp/memory/enable_shared_from_this), [std::allocator_traits](https://en.cppreference.com/w/cpp/memory/allocator_traits), [std::pmr::polymorphic_allocator](https://en.cppreference.com/w/cpp/memory/polymorphic_allocator), [std::pmr::memory_resource](https://en.cppreference.com/w/cpp/memory/memory_resource)
- **Prerequisites:** raii-and-resource-management, object-lifetime-and-storage, copy-and-move

Raw `new` and `delete` mix two distinct responsibilities: obtaining memory and tracking who destroys the object. Modern C++ splits ownership models (`std::unique_ptr`, `std::shared_ptr`, `std::weak_ptr`) from allocation strategies (`std::allocator_traits`, `std::pmr::memory_resource`), giving you deterministic teardown without hidden runtime costs.

## Exclusive ownership and deleter footprint

`std::unique_ptr<T, Deleter>` models single, non-copyable ownership. Move operations transfer the wrapped pointer and null the source in two register instructions, and the destructor invokes `Deleter` if the stored pointer is non-null. Because the deleter is a template parameter baked into the type, the compiler optimizes empty stateless deleters—such as `std::default_delete<T>`, empty function objects, or captureless lambdas—via empty-base optimization or `[[no_unique_address]]`, making `sizeof(std::unique_ptr<T>) == sizeof(T*)`.

When you pass a function pointer (`void(*)(T*)`) or a stateful capturing lambda as `Deleter`, that state lives inside the `unique_ptr` itself, doubling or growing its footprint and changing its static type. For array buffers (`std::unique_ptr<T[]>`), the specialization provides `operator[]`, omits `*` and `->`, dispatches `delete[]`, and blocks derived-to-base pointer conversions because array indexing scales by `sizeof(Base)`. Since C++20, `std::make_unique_for_overwrite<T[]>(n)` lets you allocate large uninitialized arrays without paying for a forced zero-fill.

> **Mental model.** A `unique_ptr` with a stateless deleter compiles to a single machine pointer; only its deleted copy operations and deterministic destructor distinguish it from `T*`.

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

struct HandleCloser {
    void operator()(int* p) const noexcept { delete p; }
};

void close_raw(int* p) noexcept { delete p; }

static_assert(sizeof(std::unique_ptr<int>) == sizeof(int*));
static_assert(sizeof(std::unique_ptr<int, HandleCloser>) == sizeof(int*));
static_assert(sizeof(std::unique_ptr<int, void(*)(int*)>) == 2 * sizeof(int*));

auto scratch = std::make_unique_for_overwrite<std::byte[]>(4096);
```

## Shared ownership and the control block

Unlike `unique_ptr`, `std::shared_ptr<T>` is always two pointers wide (`sizeof == 2 * sizeof(void*)`): the **stored pointer** returned by `get()` and a pointer to a heap-allocated **control block**. That control block holds four ingredients: an atomic strong reference count (`use_count`), an atomic weak reference count (`weak_count`), a type-erased deleter, and a copy of the allocator used to create the control block itself.

Keeping the stored pointer separate from the control block unlocks two crucial features. First, converting `std::shared_ptr<Derived>` to `std::shared_ptr<Base>` works safely even when `Base` has a **non-virtual** destructor or uses multiple inheritance, because the control block's type-erased deleter remembers the original `Derived*` passed at construction while the stored pointer shifts to the `Base` subobject. Second, the **aliasing constructor** `std::shared_ptr<Member>(owner, &owner->member)` creates a handle pointing directly to a subobject while still keeping `owner`'s control block alive.

Prefer `std::make_shared<T>(args...)` or `std::allocate_shared<T>(alloc, args...)` whenever possible: both allocate a single contiguous block holding the control block alongside `T`'s storage, cutting allocation count in half and improving cache locality.

> **Pitfall.** Never construct two separate `shared_ptr` instances from the same raw pointer `T*`; each creates an independent control block and triggers double deletion. Notice too that `std::make_shared` fuses the control block and `T` into one allocation: although `~T()` runs immediately when the strong count drops to zero, the raw memory block cannot be freed until every surviving `weak_ptr` also expires.

```cpp
#include <memory>

struct Base { int tag = 1; }; // Intentionally non-virtual destructor
struct Derived : Base { int payload = 42; };

struct Session : std::enable_shared_from_this<Session> {
    std::shared_ptr<Session> share() { return shared_from_this(); }
};

void ownership_demo() {
    std::shared_ptr<Base> base = std::make_shared<Derived>();
    auto derived = std::make_shared<Derived>();
    // Aliasing constructor: points to member while sharing Derived's control block
    std::shared_ptr<int> alias(derived, &derived->payload);
    static_assert(sizeof(alias) == 2 * sizeof(void*));
}
```

## Weak observers and shared_from_this

`std::weak_ptr<T>` points to the control block without incrementing the strong count, breaking parent-child ownership cycles. Because another thread can drop the last `shared_ptr` immediately after `wp.expired()` returns `false`, never gate access on `expired()`. Always call `wp.lock()`, which atomically checks `use_count > 0` and increments it in one indivisible step, returning either an owning `shared_ptr<T>` or null.

Remember the thread-safety boundary of `shared_ptr`: control-block reference counts are atomic, so copying or destroying **distinct** `shared_ptr` instances sharing one control block across threads is race-free. Reading and writing the **same** `shared_ptr` object concurrently from two threads is still a data race; wrap shared handles in `std::atomic<std::shared_ptr<T>>` (C++20) rather than the deprecated `std::atomic_load` free functions.

When a managed object must hand out `shared_ptr` handles to `*this`, inherit publicly from `std::enable_shared_from_this<T>`. Its hidden `weak_this` member is populated by `shared_ptr` constructors *after* `T` finishes constructing. Calling `shared_from_this()` inside a constructor or on an object owned by the stack or `unique_ptr` throws `std::bad_weak_ptr` (well-defined since C++17).

## Allocator traits and propagation contracts

Standard containers do not invoke allocator methods directly; they talk through `std::allocator_traits<Alloc>`, which supplies defaults for `pointer`, `rebind_alloc`, `construct`, `destroy`, `max_size`, and C++23 `allocate_at_least`. `allocate_at_least(alloc, n)` returns an `allocation_result` containing `{ptr, count}`, allowing `std::vector` to adopt extra bucket capacity rounded up by slab allocators instead of wasting trailing bytes.

For stateful allocators—such as arenas or per-subsystem pools—four propagation traits govern container operations:

- **`propagate_on_container_copy_assignment` (POCCA)** decides whether `a = b` copies `b`'s allocator handle into `a`.
- **`propagate_on_container_move_assignment` (POCMA)** decides whether `a = std::move(b)` transfers `b`'s allocator handle so `a` can steal `b`'s buffer in constant time.
- **`propagate_on_container_swap` (POCS)** decides whether `swap(a, b)` exchanges allocator handles along with buffers.
- **`select_on_container_copy_construction(alloc)`** returns the allocator used by a freshly copy-constructed container.

If `POCMA` is `std::false_type` and two containers hold unequal allocators (`a.get_allocator() != b.get_allocator()`), `a = std::move(b)` **cannot** steal `b`'s heap buffer—`a`'s allocator could not legally free memory owned by `b`'s arena—so the container falls back to linear element-by-element moves into fresh allocations.

> **Standardese.** Under `[allocator.requirements]` and `[container.requirements]`, calling `swap` on two containers whose allocators compare unequal when `propagate_on_container_swap` is `std::false_type` causes **undefined behavior** rather than copying elements.

## Type-erased allocators with std::pmr

Template allocators bake allocation strategy into the container's static type: `std::vector<int, ArenaAlloc>` cannot be passed to a function expecting `std::vector<int>`. The `<memory_resource>` library erases the allocator type behind a virtual interface, `std::pmr::memory_resource` (`do_allocate`, `do_deallocate`, `do_is_equal`). Every `std::pmr::vector<int>` uses `std::pmr::polymorphic_allocator<int>`, which stores a single `memory_resource*` pointer, keeping the container's ABI uniform whether backed by a stack buffer (`monotonic_buffer_resource`), a thread-local pool (`unsynchronized_pool_resource`), or a mutex-protected pool (`synchronized_pool_resource`).

> **Pitfall.** `std::pmr::polymorphic_allocator` sets `POCCA`, `POCMA`, and `POCS` to `std::false_type`, and `select_on_container_copy_construction()` returns a handle to `std::pmr::get_default_resource()`. Move-assigning a `pmr::vector` backed by a stack arena into a `pmr::vector` bound to a different resource silently relocates elements one by one into the target's resource instead of stealing the buffer pointer.

```cpp
#include <array>
#include <cstddef>
#include <memory_resource>
#include <vector>

void pmr_arena_demo() {
    std::array<std::byte, 1024> stack_bytes{};
    std::pmr::monotonic_buffer_resource arena{
        stack_bytes.data(), stack_bytes.size(), std::pmr::null_memory_resource()
    };

    std::pmr::vector<int> local(&arena);
    local.push_back(10);
    local.push_back(20);

    std::pmr::vector<int> heap_vec; // Uses std::pmr::get_default_resource()
    heap_vec = std::move(local);    // POCMA is false: copies/moves elements, does NOT steal arena
}
```

The memory-management choices summarize cleanly across footprint, allocation count, and strategy propagation:

| Mechanism | Footprint | Heap Control Block | Type-Erased Strategy | Propagation / Safety Rule |
| --- | --- | --- | --- | --- |
| `unique_ptr<T>` | 1 pointer | None | No (deleter in type) | Move-only; stateless deleter has zero size cost |
| `shared_ptr<T>` | 2 pointers | Yes (atomic counts) | Yes (deleter + allocator) | `make_shared` fuses object and control block |
| `weak_ptr<T>` | 2 pointers | Observes control block | Yes | Always promote via `lock()`, never `expired()` |
| Custom `Allocator` | Zero or state | None | No (infects container type) | Check `POCMA` and `POCS` on stateful arenas |
| `pmr::polymorphic_allocator` | 1 pointer | None | Yes (`memory_resource*`) | Never propagates on copy, move-assign, or swap |

## What to remember

- Reach for `std::unique_ptr` by default; stateless deleters add zero size overhead over `T*`, while function-pointer deleters double the pointer size.
- A `std::shared_ptr` carries two pointers (stored pointer plus control block), making aliasing and non-virtual base destruction safe, while `make_shared` trades deferred memory release alongside `weak_ptr` for a single allocation.
- Promote `std::weak_ptr` strictly with `lock()`, and never call `shared_from_this()` inside constructors or on objects not yet adopted by a `shared_ptr`.
- Use `std::allocator_traits` in generic containers and watch `POCMA` and `POCS` whenever allocators carry state.
- `std::pmr` unifies container types across arenas and pools, but non-propagating `polymorphic_allocator` contracts turn cross-resource move assignment into element-by-element reallocation.

## Concept Map Outline

- **Smart pointers & allocators:** C++ decouples deterministic object ownership from raw memory allocation strategies.
  - **Exclusive ownership (unique_ptr):** Move-only handle encoding cleanup inside its deleter type parameter.
    - **Zero-size stateless deleter:** Default or empty functor deleters keep sizeof(unique_ptr<T>) equal to T*.
    - **Stateful or function deleters:** Function pointers or capturing lambdas grow unique_ptr beyond one pointer.
    - **Array specialization T[]:** Provides operator[], calls delete[], and blocks derived-to-base array conversion.
  - **Shared ownership (shared_ptr):** Two-pointer reference-counted handle sharing a heap control block.
    - **Two-pointer layout:** Stores both get() and control-block pointer, enabling aliasing and base casts.
    - **Control block fields:** Tracks atomic strong count, weak count, type-erased deleter, and allocator.
    - **make_shared single allocation:** Fuses T storage and control block until the last weak_ptr also expires.
  - **Weak observation & thread safety:** Non-owning observers break cycles and inspect control blocks safely.
    - **Atomic promotion via lock():** Checks and increments use_count atomically, avoiding expired() TOCTOU races.
    - **enable_shared_from_this:** Initializes weak_this after construction; throws bad_weak_ptr if unowned.
    - **atomic<shared_ptr<T>>:** C++20 wrapper required when mutating the same shared_ptr handle across threads.
  - **Allocator traits & contracts:** allocator_traits normalizes container-to-allocator interaction and propagation.
    - **Trait defaults & allocate_at_least:** Synthesizes construct/destroy and lets containers claim over-allocated buckets.
    - **POCCA, POCMA & POCS:** False POCMA forces O(N) moves across unequal arenas; unequal false POCS swap is UB.
  - **Polymorphic memory resources:** std::pmr erases allocator types behind a memory_resource* pointer.
    - **Uniform container types:** All std::pmr::vector<T> share one static type across arenas and pools.
    - **monotonic_buffer_resource:** Fast bump allocator over stack or upstream buffers freed wholesale on teardown.
    - **Non-propagating pmr handles:** polymorphic_allocator sets POCCA, POCMA, and POCS false to preserve arena identity.

## Standardese Quiz Questions

### Question 1 [warmup] ([unique.ptr.single.general])

On a 64-bit platform (8-byte pointers), what are the sizes of p1 (empty functor deleter) and p2 (function pointer deleter)?

```cpp
struct EmptyDeleter { void operator()(int* p) const noexcept { delete p; } };
void fn_deleter(int* p) noexcept { delete p; }

std::unique_ptr<int, EmptyDeleter> p1;
std::unique_ptr<int, void(*)(int*)> p2(nullptr, fn_deleter);
```

A. Both p1 and p2 are 8 bytes
B. p1 is 8 bytes and p2 is 16 bytes
C. p1 is 16 bytes and p2 is 8 bytes
D. Both p1 and p2 are 16 bytes

**Answer (B):** p1 is 8 bytes and p2 is 16 bytes

**Why:** std::unique_ptr compresses an empty stateless functor deleter (like EmptyDeleter or std::default_delete) to zero extra bytes via empty-base optimization or [[no_unique_address]], so p1 stays 8 bytes. A function pointer void(*)(int*) carries runtime address state stored inside p2, doubling its size to 16 bytes.

### Question 2 [warmup] ([util.smartptr.weak.obs])

Two threads share access to an object observed through std::weak_ptr<Session> wp. What is the correct way to safely use the Session if it is still alive?

A. Call if (auto sp = wp.lock()) { sp->ping(); }
B. Call if (!wp.expired()) { wp.lock()->ping(); }
C. Cast wp with std::static_pointer_cast<Session>
D. Read wp.use_count() > 0 before calling wp.lock()

**Answer (A):** Call if (auto sp = wp.lock()) { sp->ping(); }

**Why:** Checking expired() or use_count() before locking creates a time-of-check-to-time-of-use race: another thread can destroy the final shared_ptr in between, causing wp.lock() to return null. Calling wp.lock() directly atomically checks and increments the strong reference count.

### Question 3 [tricky] ([util.smartptr.shared.create])

A BigBuffer object is created via std::make_shared<BigBuffer>(), a std::weak_ptr<BigBuffer> observer is saved, and all std::shared_ptr handles are then destroyed while observer stays alive. What happens to BigBuffer's destructor and its heap memory?

```cpp
auto sp = std::make_shared<BigBuffer>();
std::weak_ptr<BigBuffer> observer = sp;
sp.reset();
// observer is still alive here
```

A. Neither ~BigBuffer() runs nor is any memory freed until observer is destroyed
B. Both ~BigBuffer() runs and the entire heap allocation is freed immediately on sp.reset()
C. ~BigBuffer() runs immediately on sp.reset(), but the underlying memory block stays allocated until observer expires
D. observer.lock() reconstructs BigBuffer on demand from the control block

**Answer (C):** ~BigBuffer() runs immediately on sp.reset(), but the underlying memory block stays allocated until observer expires

**Why:** std::make_shared allocates the control block and BigBuffer in a single contiguous heap allocation. When the strong count hits zero, ~BigBuffer() is invoked immediately, yet the combined memory chunk cannot be passed to deallocate until weak_count also reaches zero.

### Question 4 [tricky] ([mem.poly.allocator.class], [container.alloc.reqmts])

In the snippet below, src allocates from a stack monotonic_buffer_resource, whereas dst uses std::pmr::get_default_resource(). What happens during dst = std::move(src);?

```cpp
std::array<std::byte, 512> buf{};
std::pmr::monotonic_buffer_resource arena{buf.data(), buf.size()};
std::pmr::vector<int> src(&arena);
src.assign({1, 2, 3, 4});

std::pmr::vector<int> dst; // default memory resource
dst = std::move(src);
```

A. dst steals src's buffer pointer in O(1) time and adopts arena as its allocator
B. Compilation fails because pmr::vector objects with different resources have incompatible types
C. Undefined behavior because arena and the default resource compare unequal
D. dst retains the default memory resource and moves/copies the elements into newly allocated storage because polymorphic_allocator's POCMA is false_type

**Answer (D):** dst retains the default memory resource and moves/copies the elements into newly allocated storage because polymorphic_allocator's POCMA is false_type

**Why:** std::pmr::polymorphic_allocator defines propagate_on_container_move_assignment as std::false_type. Because dst.get_allocator() != src.get_allocator(), move assignment cannot transfer ownership of src's arena buffer and instead moves elements individually into memory allocated by dst's resource.

### Question 5 [standardese] ([util.smartptr.shared.const], [expr.delete])

Base has a non-virtual destructor and Derived publicly inherits from Base. Compare destroying sp (std::shared_ptr<Base>) vs destroying up (std::unique_ptr<Base>) when both were initialized from new Derived.

```cpp
struct Base { int a = 1; }; // non-virtual ~Base()
struct Derived : Base { std::string s = "data"; };

std::shared_ptr<Base> sp(new Derived);
std::unique_ptr<Base> up(new Derived);
```

A. Both invoke undefined behavior when destroyed because Base lacks a virtual destructor
B. sp cleanly invokes ~Derived() via its control block's type-erased deleter, whereas up invokes undefined behavior (delete on Base* with non-virtual destructor)
C. up cleanly invokes ~Derived() via default_delete, whereas sp leaks s
D. Both safely invoke ~Derived() in C++20 and later

**Answer (B):** sp cleanly invokes ~Derived() via its control block's type-erased deleter, whereas up invokes undefined behavior (delete on Base* with non-virtual destructor)

**Why:** std::shared_ptr<Base>(new Derived) captures the static type Derived* inside its control block's type-erased deleter at construction time, so teardown deletes the complete Derived object safely. By contrast, std::unique_ptr<Base> stores only std::default_delete<Base>, calling delete static_cast<Base*>(p), which is undefined behavior under [expr.delete] when ~Base() is non-virtual.

### Question 6 [standardese] ([util.smartptr.enab])

A class Node publicly inherits from std::enable_shared_from_this<Node>. In C++17 and later, what happens if Node::Node() calls shared_from_this() inside its constructor body?

```cpp
struct Node : std::enable_shared_from_this<Node> {
    Node() { auto self = shared_from_this(); }
};
auto p = std::make_shared<Node>();
```

A. Returns a valid std::shared_ptr<Node> sharing the incoming make_shared control block
B. Silent undefined behavior with a null control block
C. Throws std::bad_weak_ptr because weak_this is not initialized until after Node construction finishes
D. Fails to compile unless Node is created via std::shared_ptr<Node>(new Node)

**Answer (C):** Throws std::bad_weak_ptr because weak_this is not initialized until after Node construction finishes

**Why:** The std::shared_ptr constructor populates enable_shared_from_this's internal weak_this member only after the managed Node object has finished constructing. Under C++17+ [util.smartptr.enab], shared_from_this() is specified as return shared_ptr<T>(weak_this), which throws std::bad_weak_ptr when weak_this is empty.
