# Smart pointers

> unique_ptr makes single ownership a property of the type at no run-time cost, shared_ptr counts owners in a separate control block, and weak_ptr observes without owning. This page covers what each one stores, where the deleter lives, which operations are thread-safe, and the bugs that come from owning one object twice.

- **Source:** C++ Atlas (https://cppatlas.com/topics/smart-pointers)
- **Track:** Memory model
- **Estimated reading time:** 14 minutes
- **ISO C++ clauses:** [unique.ptr] (https://eel.is/c++draft/unique.ptr), [unique.ptr.dltr] (https://eel.is/c++draft/unique.ptr.dltr), [util.smartptr.shared] (https://eel.is/c++draft/util.smartptr.shared), [util.smartptr.shared.const] (https://eel.is/c++draft/util.smartptr.shared.const), [util.smartptr.shared.create] (https://eel.is/c++draft/util.smartptr.shared.create), [util.smartptr.weak] (https://eel.is/c++draft/util.smartptr.weak), [util.smartptr.enab] (https://eel.is/c++draft/util.smartptr.enab), [util.smartptr.atomic] (https://eel.is/c++draft/util.smartptr.atomic), [smartptr.adapt] (https://eel.is/c++draft/smartptr.adapt)
- **cppreference links:** [std::unique_ptr](https://en.cppreference.com/w/cpp/memory/unique_ptr), [std::shared_ptr](https://en.cppreference.com/w/cpp/memory/shared_ptr), [std::shared_ptr constructors](https://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr), [std::make_shared, std::make_shared_for_overwrite](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::out_ptr_t](https://en.cppreference.com/w/cpp/memory/out_ptr_t)
- **Prerequisites:** raii-and-resource-management, copy-and-move, dynamic-allocation-new-and-delete

## The one idea

A smart pointer is a class that owns an object and releases it in its destructor. The three standard ones differ in where the knowledge of ownership lives. For `std::unique_ptr<T, D>` it lives in the type: there is exactly one owner, the deleter is a template argument, and the object is a pointer plus whatever state the deleter needs. For `std::shared_ptr<T>` it lives in a separate heap object, the **control block**, which counts owners and remembers how to destroy the object. `std::weak_ptr<T>` points at the same control block without contributing to the owner count.

> **Mental model.** A `unique_ptr` is a raw pointer with a destructor and a deleted copy constructor. A `shared_ptr` is two raw pointers: one to the object you dereference, and one to a control block that decides when, and how, something gets destroyed. Those two pointers need not agree, and most of the surprises come from that.

The general case for tying resources to scopes is the topic on RAII; this page is about the specific tools.

## unique_ptr: ownership in the type

`unique_ptr` is move-only. Moving transfers the pointer and leaves the source null, which the standard guarantees, unlike moved-from states in general. The deleter is part of the type, so `unique_ptr<FILE, int(*)(FILE*)>` and `unique_ptr<FILE, Closer>` are different types.

The deleter is stored inside the `unique_ptr`. An empty class deleter, such as the default `std::default_delete<T>` or a captureless lambda, costs nothing in practice, because libstdc++, libc++ and MSVC all use the empty base optimization or `[[no_unique_address]]`. The standard does not promise a size. A function pointer deleter, by contrast, must be stored, so it makes the object two pointers wide on every mainstream ABI.

```cpp
#include <cstdio>
#include <memory>

struct Closer { void operator()(std::FILE* f) const { std::fclose(f); } };

using File  = std::unique_ptr<std::FILE, Closer>;               // stateless deleter
using FileP = std::unique_ptr<std::FILE, int (*)(std::FILE*)>;  // stores a function pointer

static_assert(sizeof(File) == sizeof(std::FILE*));     // holds on libstdc++ and libc++, not guaranteed
static_assert(sizeof(FileP) == 2 * sizeof(std::FILE*));  // the function pointer is stored
```

`unique_ptr<T[]>` is a separate partial specialization. Its default deleter calls `delete[]`, it provides `operator[]` instead of `operator*` and `operator->`, and it refuses conversions from pointers to derived types, because deleting an array through a base pointer is undefined. `std::make_unique<T[]>(n)` value-initializes the elements; C++20's `std::make_unique_for_overwrite` default-initializes them, which skips zeroing for trivial types.

### Incomplete types and the out-of-line destructor

`unique_ptr<T>` may be declared with an incomplete `T`, which is what makes it the standard pimpl member. But `default_delete<T>::operator()` requires a complete type, and the implicitly defined destructor of the enclosing class calls it wherever that destructor is defined. If the class's destructor is implicit, it is defined inline in every translation unit that destroys one, where `Impl` is incomplete, and the program is ill-formed.

```cpp
#include <memory>

// widget.h
struct Impl;
struct Widget {
  Widget();
  ~Widget();                 // declared here, defined where Impl is complete
  std::unique_ptr<Impl> p;
};

// widget.cpp
struct Impl { int x = 0; };
Widget::Widget() : p(std::make_unique<Impl>()) {}
Widget::~Widget() = default;  // default_delete<Impl> is instantiated here
```

The same applies to a move assignment operator, which also destroys the old `Impl`. `shared_ptr` has no such requirement, because its constructor captures the deleter into the control block while the type is complete.

## shared_ptr: the control block

The control block holds the strong count (the number of `shared_ptr` owners), the weak count, the deleter and the allocator. When the strong count reaches zero, the object is destroyed by the stored deleter. When the weak count also reaches zero, the control block itself is freed.

Because the deleter is type-erased in the control block, two `shared_ptr<T>` with different deleters have the same type. A less obvious consequence is that the deleter is chosen from the pointer type you passed to the constructor, not from `T`. A `shared_ptr<Base>` built from `new Derived` deletes a `Derived*`, so it calls the correct destructor even when `Base` has no virtual destructor. A `shared_ptr<void>` destroys its object correctly for the same reason.

```cpp
#include <cstdio>
#include <memory>

struct Base    { ~Base()    { std::puts("~Base"); } };     // not virtual
struct Derived : Base { ~Derived() { std::puts("~Derived"); } };

int main() {
  std::shared_ptr<Base> p(new Derived);
  p.reset();                         // prints ~Derived, ~Base
  // std::unique_ptr<Base> u(new Derived); would be undefined when u is destroyed,
  // because default_delete<Base> runs delete on a Base*
}
```

### make_shared trade-offs

`std::make_shared<T>(args...)` is recommended to perform a single allocation holding both the control block and the object. That halves the allocations and improves locality, and it was also the fix for exception-unsafe argument lists before C++17. The costs are real, though.

- The object's storage is part of the control block, so it cannot be returned until the last `weak_ptr` is gone, even though the object is destroyed when the last `shared_ptr` goes. A large object observed by a long-lived `weak_ptr` keeps its memory.
- You cannot supply a custom deleter.
- The constructor must be accessible from inside the library, so a class with a private constructor and a static factory cannot use it directly.
- It initializes with parentheses. Since C++20, parenthesized aggregate initialization makes aggregates work too.

C++20 added `make_shared<T[]>` for arrays and `make_shared_for_overwrite`. `std::allocate_shared` takes an allocator for the combined block.

### The aliasing constructor

`shared_ptr<T>(const shared_ptr<U>& owner, T* p)` shares `owner`'s control block but stores `p`. The result keeps the whole owner alive while pointing into it, which is how you hand out a `shared_ptr` to a member.

```cpp
#include <cstdio>
#include <memory>
#include <utility>

int main() {
  auto owner = std::make_shared<std::pair<int, int>>(1, 2);
  std::shared_ptr<int> second(owner, &owner->second);
  owner.reset();
  std::printf("%d %ld\n", *second, second.use_count());   // prints 2 1
}
```

Nothing checks that `p` lies inside the owned object. Aliasing an empty `shared_ptr` with a non-null `p` even yields a pointer that dereferences fine and has a `use_count()` of zero.

## Thread safety: the count, not the pointee

The control block's counts are updated atomically, so different `shared_ptr` objects that share ownership may be copied, assigned and destroyed concurrently in different threads. The standard words this narrowly: for data-race purposes, the member functions access only the `shared_ptr` and `weak_ptr` objects themselves, not the objects they point to.

That leaves two things unprotected. The pointee is an ordinary object, so concurrent unsynchronized writes to `*p` race. And a single `shared_ptr` object is an ordinary object too: one thread assigning to `g` while another copies `g` is a data race on `g`, even though the counts are atomic.

> **Pitfall.** "shared_ptr is thread-safe" is true of the reference count and false of everything else. Sharing one global `shared_ptr` that some thread reassigns needs a mutex or `std::atomic<std::shared_ptr<T>>`. The atomic specialization arrived in C++20 and deprecated the old `std::atomic_load` free functions, which C++26 removes. The topic on atomics and memory ordering covers the orderings.

## weak_ptr, cycles and enable_shared_from_this

Two objects that hold `shared_ptr`s to each other never reach a strong count of zero, so neither is destroyed. Break the cycle by making the back edge, typically child to parent or observer to subject, a `weak_ptr`. To use it, call `lock()`, which atomically returns either an owning `shared_ptr` or an empty one. `expired()` is only a hint in multithreaded code, since the answer can change before you act on it.

An object that wants a `shared_ptr` to itself cannot write `std::shared_ptr<T>(this)`, because that creates a second control block. Instead it derives publicly from `std::enable_shared_from_this<T>`, which holds a `weak_ptr` that the first owning `shared_ptr` constructor fills in. `shared_from_this()` locks it.

```cpp
#include <cstdio>
#include <memory>

struct Node : std::enable_shared_from_this<Node> {};

int main() {
  auto owned = std::make_shared<Node>();
  auto again = owned->shared_from_this();      // shares owned's control block
  Node stack_node;
  try { stack_node.shared_from_this(); }
  catch (const std::bad_weak_ptr&) { std::puts("bad_weak_ptr"); }   // since C++17
}
```

Before C++17 calling `shared_from_this()` on an object that no `shared_ptr` owned was undefined; since C++17 it throws `std::bad_weak_ptr`. It also fails inside the constructor, because no `shared_ptr` owns the object yet. C++17 added `weak_from_this()`, which returns an empty `weak_ptr` in that case instead of throwing.

## Smart pointers in interfaces

The signature should say what the function does with ownership.

| Parameter | Meaning |
|---|---|
| `T&` or `T*` | uses the object, no ownership involved; `T*` if it may be null |
| `std::unique_ptr<T>` by value | takes ownership; the caller must `std::move` |
| `std::unique_ptr<T>&` | may reseat the caller's pointer |
| `std::shared_ptr<T>` by value | keeps a share; move it into storage |
| `const std::shared_ptr<T>&` | might keep a share, decided inside |

Passing `shared_ptr` by value to a function that only reads the object costs two atomic operations for nothing. Returning a `unique_ptr` from a factory is the most flexible choice, because it converts implicitly to `shared_ptr`.

C++23 added `std::out_ptr` and `std::inout_ptr` for C APIs that return a resource through a `T**` parameter. `std::out_ptr(p)` resets `p` when the adapter is constructed, passes the address of a temporary pointer, and calls `p.reset(new_value)` when the adapter is destroyed at the end of the full-expression. `std::inout_ptr(p)` instead releases `p` without deleting, passing the old value in so the API can free or reallocate it. With a `shared_ptr`, `out_ptr` requires an explicit deleter argument, and `inout_ptr` is not allowed at all, because a `shared_ptr` cannot release ownership.

```cpp
#include <cstdlib>
#include <memory>

extern "C" int c_make(int** out);   // allocates *out with malloc

struct Free { void operator()(int* p) const { std::free(p); } };

void use() {
  std::unique_ptr<int, Free> p;
  c_make(std::out_ptr(p));          // p owns the result after this full-expression
}
```

## Owning one object twice

Almost every smart-pointer crash is two owners that do not know about each other.

- Constructing two `shared_ptr`s from the same raw pointer creates two control blocks, and each deletes the object.
- `std::shared_ptr<T>(this)` inside a member function does the same thing whenever the object is already owned.
- Wrapping `p.get()` in a new smart pointer double-deletes.
- A `unique_ptr` or `shared_ptr` to an object with automatic or static storage duration deletes something `new` never made.

> **Standardese.** [util.smartptr.shared.const] says that constructing a `shared_ptr` from a raw pointer `p` produces an object that owns `p` with `use_count() == 1`. Nothing in that specification consults any other `shared_ptr`, so two constructions from the same pointer give two owners that each believe they are alone, and [util.smartptr.shared.dest] then has each of them delete `p`. The second deletion is undefined.

The fixes are mechanical: create objects with `make_unique` or `make_shared` so a raw owning pointer never exists, pass `shared_ptr` copies rather than raw pointers when ownership must be shared, and use `enable_shared_from_this` for self-references.

## What to remember

- `unique_ptr` is move-only, a moved-from one is null, and its deleter is part of the type; empty deleters cost no space in practice, but the standard promises no size.
- `unique_ptr<T[]>` calls `delete[]`, has `operator[]`, and refuses derived-to-base conversions.
- A pimpl `unique_ptr<Impl>` needs the enclosing class's destructor, and move assignment, defined where `Impl` is complete.
- `shared_ptr` keeps the deleter in the control block, chosen from the constructor's pointer type, so `shared_ptr<Base>(new Derived)` destroys correctly without a virtual destructor.
- `make_shared` makes one allocation, but the object's memory outlives it until the last `weak_ptr` is gone.
- The aliasing constructor shares ownership with one object while pointing at another.
- Counts are atomic; the pointee and a single shared `shared_ptr` object are not. Use `std::atomic<std::shared_ptr<T>>` (C++20) for the latter.
- Break cycles with `weak_ptr` and `lock()`. `shared_from_this()` on an unowned object throws `bad_weak_ptr` since C++17.
- `out_ptr` and `inout_ptr` (C++23) adapt smart pointers to `T**` C APIs and update the pointer at the end of the full-expression.
- Never create a second owner from a raw pointer that already has one.

## Concept Map Outline

- **Smart pointers:** unique_ptr owns alone, shared_ptr counts owners in a control block, weak_ptr observes.
  - **unique_ptr:** Move-only single owner; the deleter is part of the type.
    - **Moved-from is null:** The standard guarantees a moved-from unique_ptr holds nullptr.
    - **Deleter size:** Empty deleters cost nothing in practice; a function pointer deleter is stored. No size is guaranteed.
    - **unique_ptr<T[]>:** Calls delete[], offers operator[], and rejects derived-to-base conversions.
    - **Pimpl destructor:** Declare ~Widget in the header and define it where Impl is complete.
  - **shared_ptr:** An object pointer plus a control block pointer; the two need not agree.
    - **Control block:** Holds strong count, weak count, deleter and allocator.
    - **Deleter from ctor type:** shared_ptr<Base>(new Derived) deletes a Derived*, even without a virtual destructor.
    - **make_shared:** One allocation; the object's memory stays until the last weak_ptr is gone.
    - **Aliasing constructor:** Shares one object's ownership while pointing at another, such as a member.
  - **Thread safety:** Only the reference counts are synchronized.
    - **Distinct copies:** Different shared_ptr objects sharing a block may be used concurrently.
    - **One shared object:** Reading and assigning the same shared_ptr from two threads is a data race.
    - **atomic<shared_ptr> (C++20):** Replaces the deprecated atomic_load free functions, removed in C++26.
  - **weak_ptr:** Points at the control block without keeping the object alive.
    - **Breaking cycles:** Make back edges weak so strong counts can reach zero.
    - **lock():** Atomically yields an owner or an empty shared_ptr; expired() is only a hint.
    - **shared_from_this:** Needs an existing owner; throws bad_weak_ptr since C++17, UB before.
    - **weak_from_this (C++17):** Returns an empty weak_ptr instead of throwing when unowned.
  - **Interfaces:** The parameter type states what happens to ownership.
    - **T& or T*:** Use without ownership; prefer these when the callee only reads or writes the object.
    - **unique_ptr by value:** A sink: the callee takes ownership and the caller must move.
    - **out_ptr (C++23):** Resets at construction, stores the T** result at the end of the full-expression.
    - **inout_ptr (C++23):** Releases the old pointer into the call; not usable with shared_ptr.
  - **Double ownership:** Two owners that do not know about each other delete twice.
    - **Two from one raw:** Each shared_ptr built from the same raw pointer makes its own control block.
    - **shared_ptr(this):** Creates a second control block; use enable_shared_from_this.
    - **Wrapping get():** A new owner of p.get() deletes what p will delete again.

## Standardese Quiz Questions

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

Is this program well-formed?

```cpp
#include <memory>

int main() {
  auto a = std::make_unique<int>(1);
  auto b = a;
  (void)b;
}
```

A. Well-formed: `b` shares ownership with `a`
B. Well-formed: `a` is moved into `b` and left null
C. Ill-formed: the copy constructor of `std::unique_ptr` is deleted
D. Undefined behavior: the `int` is deleted twice

**Answer (C):** Ill-formed: the copy constructor of `std::unique_ptr` is deleted

**Why:** `a` is an lvalue, so `auto b = a;` selects the copy constructor, which `std::unique_ptr` declares as deleted. Ownership only moves when you ask for it with `std::move(a)`, after which `a` is guaranteed to be null. The second option describes the removed `std::auto_ptr`, whose copy constructor silently transferred ownership.

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

What does this program print?

```cpp
#include <cstdio>
#include <memory>

int main() {
  auto p = std::make_shared<int>(1);
  auto q = p;
  std::weak_ptr<int> w = p;
  p.reset();
  std::printf("%ld %ld %d\n", q.use_count(), w.use_count(), w.expired());
}
```

A. `1 0 0`
B. `2 1 0`
C. `1 1 1`
D. `1 1 0`

**Answer (D):** `1 1 0`

**Why:** After `p.reset()`, `q` is the only owner, so the strong count is 1. `weak_ptr::use_count()` reports the number of `shared_ptr` owners of the shared object, not the number of weak references, so it is also 1. Since an owner remains, `expired()` is false. Reading `w.use_count()` as the weak count is the usual mistake behind the first option.

### Question 3 [warmup] ([util.smartptr.enab], [util.smartptr.shared.const])

In C++17 and later, what does this program print?

```cpp
#include <cstdio>
#include <memory>

struct Node : std::enable_shared_from_this<Node> {};

int main() {
  Node n;
  try {
    auto p = n.shared_from_this();
    std::puts(p ? "owner" : "empty");
  } catch (const std::bad_weak_ptr&) {
    std::puts("bad_weak_ptr");
  }
}
```

A. `bad_weak_ptr`
B. `empty`
C. `owner`
D. Nothing predictable: the behavior is undefined

**Answer (A):** `bad_weak_ptr`

**Why:** `shared_from_this()` locks the `weak_ptr` stored in the `enable_shared_from_this` base. No `shared_ptr` ever owned `n`, so that `weak_ptr` is empty, and since C++17 the call is specified as `shared_ptr<T>(weak_this)`, whose constructor throws `std::bad_weak_ptr`. Before C++17 the call had a precondition that an owner existed, so the last option was correct in C++14. Use `weak_from_this()` if an empty result is preferable to an exception.

### Question 4 [tricky] ([util.smartptr.shared.const], [util.smartptr.shared.dest], [expr.delete])

`Base` has no virtual destructor. What does this program print?

```cpp
#include <cstdio>
#include <memory>

struct Base { ~Base() { std::puts("~Base"); } };
struct Derived : Base { ~Derived() { std::puts("~Derived"); } };

int main() {
  std::shared_ptr<Base> p(new Derived);
  p.reset();
}
```

A. `~Base` only, because the destructor is not virtual
B. `~Derived` then `~Base`
C. Nothing predictable: deleting through a base without a virtual destructor is undefined
D. Ill-formed: `shared_ptr<Base>` cannot be constructed from `Derived*`

**Answer (B):** `~Derived` then `~Base`

**Why:** The constructor `template<class Y> shared_ptr(Y* p)` records a deleter that performs `delete p` on the `Y*` it received, here `Derived*`. The control block keeps that deleter, so the eventual deletion has static type `Derived` and runs both destructors in the right order. With `std::unique_ptr<Base>` the default deleter is `default_delete<Base>`, and the third option would be correct.

### Question 5 [tricky] ([unique.ptr.dltr.dflt], [unique.ptr.single.dtor])

This is the whole translation unit; `Impl` is defined in another one. Is it well-formed?

```cpp
#include <memory>

struct Impl;

struct Widget {
  std::unique_ptr<Impl> p;
};

int main() { Widget w; }
```

A. Well-formed: `unique_ptr` is designed to hold incomplete types
B. Well-formed, but the `Impl` destructor is not called
C. Undefined behavior: deleting an incomplete type skips the destructor
D. Ill-formed: destroying `w` instantiates `default_delete<Impl>::operator()` with `Impl` incomplete

**Answer (D):** Ill-formed: destroying `w` instantiates `default_delete<Impl>::operator()` with `Impl` incomplete

**Why:** Declaring a `unique_ptr<Impl>` member with `Impl` incomplete is fine, which is why the first option is tempting. But `Widget`'s implicit destructor is defined in this translation unit, it calls `~unique_ptr`, and that calls `default_delete<Impl>::operator()`, which requires a complete type; both libstdc++ and libc++ reject it. A raw `delete` of an incomplete type would be the undefined case in the third option, and `default_delete` exists precisely to turn that into a compile error. Declare `~Widget()` here and define it as `= default` where `Impl` is complete.

### Question 6 [tricky] ([util.smartptr.shared.const])

What does this program print?

```cpp
#include <cstdio>
#include <memory>
#include <utility>

int main() {
  auto owner = std::make_shared<std::pair<int, int>>(1, 2);
  std::shared_ptr<int> second(owner, &owner->second);
  owner.reset();
  std::printf("%d %ld\n", *second, second.use_count());
}
```

A. `2 1`
B. `2 2`
C. Nothing predictable: `second` dangles once `owner` is reset
D. `2 0`

**Answer (A):** `2 1`

**Why:** The aliasing constructor makes `second` share ownership with `owner`, using the same control block, while storing a pointer to the `second` member. Resetting `owner` drops one of the two owners, so the pair stays alive and `use_count()` is 1. The dangling answer assumes `second` owns only an `int`, which it does not; a use count of 0 only happens when the aliasing source was empty.

### Question 7 [standardese] ([util.smartptr.shared], [intro.races], [res.on.data.races])

What does the standard say about this program?

```cpp
#include <memory>
#include <thread>

std::shared_ptr<int> g = std::make_shared<int>(0);

void reader() { std::shared_ptr<int> local = g; (void)local; }
void writer() { g = std::make_shared<int>(1); }

int main() {
  std::thread a(reader), b(writer);
  a.join();
  b.join();
}
```

A. Well-defined: reference count updates are atomic
B. Well-defined only where atomic operations on two pointers are lock-free
C. Data race on the `int` objects, but not on `g`
D. Data race on `g`: undefined behavior

**Answer (D):** Data race on `g`: undefined behavior

**Why:** [util.smartptr.shared] says that, for determining data races, `shared_ptr` member functions access and modify only the `shared_ptr` and `weak_ptr` objects themselves. Copying `g` reads `g`, and assigning to `g` modifies it, from two threads without synchronization, so the program has a data race on the single object `g`. The atomic count only makes it safe for different `shared_ptr` objects that share a control block to be used concurrently. Use a mutex or `std::atomic<std::shared_ptr<int>>`.

### Question 8 [standardese] ([out.ptr.t], [out.ptr])

In C++23, what does this program print, one token per line?

```cpp
#include <cstdio>
#include <cstdlib>
#include <memory>

int c_make(int** out) {
  std::puts("c_make");
  *out = static_cast<int*>(std::malloc(sizeof(int)));
  **out = 7;
  return 0;
}

struct Free {
  void operator()(int* p) const { std::puts("free"); std::free(p); }
};

int main() {
  std::unique_ptr<int, Free> p(static_cast<int*>(std::malloc(sizeof(int))));
  c_make(std::out_ptr(p));
  std::printf("%d\n", *p);
}
```

A. `c_make`, `free`, `7`, `free`
B. `c_make`, `7`, `free`, because the original allocation leaks
C. `free`, `c_make`, `7`, `free`
D. Ill-formed: `std::out_ptr` requires an empty smart pointer

**Answer (C):** `free`, `c_make`, `7`, `free`

**Why:** The `out_ptr_t` constructor calls `p.reset()` immediately, so the original allocation is freed before `c_make` runs. The function writes into the adapter's own pointer, and the adapter's destructor, at the end of the full-expression, calls `p.reset(new_value)`, which has nothing old left to free. The final `free` comes from `p`'s destructor. The first option is what you would see if the reset happened only in the destructor. `inout_ptr` differs again: it releases the old pointer without freeing it and passes it in, expecting the function to free or reuse it.
