skip to content

Is std::unique_ptr really free compared with a raw owning pointer in C++, and where does it cost anything at all?

level: seniorimportance: should knowfreq 40%

answer

  1. sizeof with an empty deleter
  2. trivial for the purposes of calls
  3. the caller's temporary and its destructor
  4. nulling the moved-from source
  5. a stateful deleter grows it

basics

~20 s

Nearly. With the default deleter it is pointer-sized in libc++ and libstdc++ and dereferences like a raw pointer, but passing it by value goes through memory under the Itanium C++ ABI, and moves and destruction add null stores and checks.

solid answer

~50 s

Storage and access are free in practice: with `std::default_delete`, libc++ and libstdc++ store the empty deleter without spending bytes, so `sizeof(std::unique_ptr<T>) == sizeof(T*)` on both, and `*p` compiles to the same load. The standard does not promise that size; it is each library's choice. The costs appear at boundaries. `unique_ptr` has a non-trivial destructor and move constructor, so the Itanium C++ ABI used by GCC and Clang outside Windows passes it by value **in memory**: the caller builds a temporary, passes its address, and destroys it after the call with a null check and an exception landing pad, where a raw pointer travels in a register. A move also writes null into the source. libc++ offers an opt-in ABI flag that marks `unique_ptr` `[[clang::trivial_abi]]`, but it is off in the default ABI. In hot, non-inlined calls, pass a `T&` or `T*` borrow instead.

code

cpp · 8 lines
cpp
#include <memory>
struct Node { int id; };
void sink_raw(Node* p);
void sink_up(std::unique_ptr<Node> p);
void call_raw(Node* p) { sink_raw(p); }
void call_up(std::unique_ptr<Node> p) { sink_up(std::move(p)); }
int read_raw(Node* p) { return p->id; }
int read_up(const std::unique_ptr<Node>& p) { return p->id; }

go deeper

for a junior

Know that unique_ptr has no reference count and is usually the size of a raw pointer.

for a middle

Explain how libraries keep an empty deleter from costing bytes and why a stateful deleter makes the pointer larger.

for a senior

Explain the calling-convention cost of passing unique_ptr by value, read the generated code to confirm it, and choose borrows for hot non-inlined paths.

for a principal

Weigh the libc++ trivial_abi option or API changes against ABI stability; most teams accept the small call cost for clearer ownership.

## The claim and what it covers "`std::unique_ptr` is a zero-cost abstraction" is mostly true, and interviewers ask senior candidates to say **where it is not**. There are three places to look: the object's size, the code generated for using it, and the code generated when it crosses a function boundary. ## Size The standard requires behaviour, not a layout, so `sizeof` is an implementation property. Both major libraries store the deleter with empty-base or `[[no_unique_address]]`-style compression: - libc++ declares the members with its `_LIBCPP_COMPRESSED_PAIR(pointer, __ptr_, deleter_type, __deleter_)` macro; - libstdc++ stores `tuple<pointer, _Dp>` inside `__uniq_ptr_impl`, and its `tuple` does not spend storage on an empty element. Measured on Apple clang 21 with libc++ on a 64-bit target: | type | sizeof | |---|---| | `Node*` | 8 | | `std::unique_ptr<Node>` | 8 | | `std::unique_ptr<Node[]>` | 8 | | `std::unique_ptr<Node, decltype(lambda)>` (captureless) | 8 | | `std::unique_ptr<Node, void(*)(Node*)>` | 16 | A deleter with state — even just a function pointer — must be stored, which is a reason to prefer stateless deleters; the details belong with custom deleters. ## Using the pointer Dereferencing, `get()`, comparison with `nullptr` and `operator->` are inline one-liners. Once the stored pointer is in a register, `p->id` is the same single load as through a `Node*`; any difference comes from how the `unique_ptr` itself reached the function. ## Crossing a function boundary This is where the cost lives. The Itanium C++ ABI (used by GCC and Clang on Linux and macOS, including Apple arm64) passes a class that is **non-trivial for the purposes of calls** — one with a non-trivial copy or move constructor or destructor — through memory: the caller allocates a temporary, passes its address, and destroys the temporary after the call. For `void call_up(std::unique_ptr<Node> p) { sink_up(std::move(p)); }` clang at `-O2` emits: 1. a load of the pointer and a store of null into the source (the move); 2. a store of the pointer into a stack temporary and a call with the temporary's address; 3. after the call, a reload, a null store, a null check and a call to `operator delete` if non-null — the temporary's destructor; 4. an exception landing pad that runs the same destructor during unwinding. The raw-pointer version `void call_raw(Node* p) { sink_raw(p); }` compiles to a single tail-call branch. libc++ documents `_LIBCPP_ABI_ENABLE_UNIQUE_PTR_TRIVIAL_ABI`, which adds `[[clang::trivial_abi]]` to make `unique_ptr` "trivial for the purpose of calls", but it is turned on only by libc++'s ABI version 2 or by a build that defines the macro for the library and everything linked with it, never in the default ABI, because it changes the calling convention. ## Does it matter? - For most code, no: the extra instructions are a few cycles next to the allocation that created the object. - Inlined calls usually lose the overhead entirely. - In a hot, non-inlined path that moves ownership on every call, it is measurable; there, borrow with `T&` or `T*` and move ownership only where it really changes hands. - Taking `const std::unique_ptr<T>&` adds a second load (pointer to the `unique_ptr`, then the object), which is worse than either alternative. ## Moves inside containers A `std::vector<std::unique_ptr<Node>>` that grows relocates its elements. Because the standard declares `unique_ptr`'s move constructor `noexcept`, the vector moves rather than copies them (and could not copy them anyway). Each relocated element costs a pointer copy plus a null store into the old slot, and the old slots' destructors then run their null checks. A `std::vector<Node*>` would copy bare pointers instead. The difference is small, but it is the same pattern as the call boundary: `unique_ptr` turns "who owns this" into a little extra code at every transfer. ## Measuring rather than guessing To check a claim about overhead on your own toolchain, compile the two versions with `-O2 -S` (or read them in a disassembler) and compare the functions side by side, as the example on this page does. Look for three things: whether the argument arrives in a register or through memory, whether a destructor call or landing pad follows the call, and whether the hot loop contains any of it after inlining. ## What it is not `unique_ptr` adds no reference count, no atomic operation and no heap allocation of its own — those are `std::shared_ptr`'s costs. Its overhead is a calling-convention detail, not a runtime bookkeeping cost.

  • Why is a std::unique_ptr passed by value in memory rather than in a register on GCC and Clang?
    Under the Itanium C++ ABI a class with a non-trivial destructor or move constructor is non-trivial for the purposes of calls, so the caller creates a temporary, passes its address and destroys it after the call. unique_ptr has both, so it cannot travel in a register the way `T*` does. libc++'s opt-in trivial_abi flag changes this, but it is off in the default ABI.
  • When does a unique_ptr become bigger than a raw pointer?
    When its deleter has state. A captureless lambda or an empty function object costs nothing in libc++ and libstdc++, but a function-pointer deleter must be stored, so `std::unique_ptr<T, void(*)(T*)>` measured 16 bytes on a 64-bit libc++ build. The standard fixes none of these sizes.

  • unique_ptr keeps a reference count like shared_ptr
  • The standard guarantees sizeof(unique_ptr<T>) equals sizeof(T*)
  • Passing unique_ptr by value costs exactly what passing T* does
  • Dereferencing a unique_ptr goes through an extra indirection
  • const unique_ptr& is the cheapest way to pass one