C++ Smart Pointers Interview Questions
Last updated 2026-11-19
Smart pointers are the modern answer to manual memory management in C++, so any interview that mentions C++11 or later will ask about them. The questions test whether you know the ownership model behind each type, the cost of shared ownership, how reference cycles leak and what make_shared and make_unique buy you.
The answers here use the standard library types only: std::unique_ptr, std::shared_ptr and std::weak_ptr. Auto_ptr was removed in C++17 and should only be mentioned as history. For the manual mechanisms these types replace, see C++ memory management questions.
| Types | unique_ptr, shared_ptr, weak_ptr |
|---|---|
| Header | <memory> |
| Standard | C++11 onwards, make_unique from C++14 |
Choosing the right one
- unique_ptr: single owner, zero overhead beyond the pointer for a stateless deleter, movable but not copyable. Default choice.
- shared_ptr: shared ownership with an atomic reference count. Use when lifetime genuinely has several owners.
- weak_ptr: non-owning observer of a shared_ptr-managed object, used to break cycles and for caches.
- Raw pointer or reference: non-owning access where the owner outlives the user.
What interviewers listen for
A good answer separates ownership from access. Passing a unique_ptr by value means transferring ownership; passing a raw pointer or reference means "use but do not own". Passing shared_ptr by value for every call adds atomic increments, so pass const shared_ptr& or a plain reference unless the callee needs to share ownership.
C++ Smart Pointers Interview Questions: questions with worked answers
1 What is a smart pointer and why use one?
easyA smart pointer is a class that wraps a raw pointer and releases the resource in its destructor, so ownership and cleanup are tied to scope (RAII). It prevents leaks on early returns and exceptions, avoids double frees by making ownership explicit, and documents intent in the type. The overloaded operator* and operator-> let it be used like a pointer.
2 Explain unique_ptr. Why can it not be copied and how do you transfer it?
mediumunique_ptr models exclusive ownership. Its copy constructor and copy assignment are deleted, so two owners of one object cannot exist. Ownership moves with std::move, leaving the source null. It can be returned from a function directly because returning a local is treated as a move. It supports arrays (unique_ptr<int[]>), custom deleters and release(), reset() and get().
cppauto make() { return std::make_unique<int>(42); } int main() { auto a = make(); // auto b = a; // error: copy deleted auto b = std::move(a); // a is now nullptr std::cout << (a == nullptr) << *b; // 142 }3 How does shared_ptr work internally? What is the control block?
mediumA shared_ptr holds two pointers: one to the object and one to a heap-allocated control block that stores the strong reference count, the weak count, the deleter and allocator. Copying increments the strong count atomically, destruction decrements it and, at zero, destroys the object; the control block itself is freed when the weak count also reaches zero. Counter updates are thread-safe, but access to the object is not. A shared_ptr is therefore usually two pointers in size.
4 What does make_shared do better than shared_ptr<T>(new T)?
mediummake_shared allocates the object and the control block in one allocation, which is faster and more cache friendly, and it is exception safe in expressions such as f(shared_ptr<T>(new T), g()) where evaluation order could leak. The downsides: the memory for the object cannot be freed until the last weak_ptr is gone because it shares a block with the control block, and it cannot use a custom deleter. make_unique (C++14) gives the same exception-safety benefit for unique_ptr.
5 What is a shared_ptr cycle and how does weak_ptr fix it? Predict the output.
hardIf A holds a shared_ptr to B and B holds a shared_ptr to A, neither strong count reaches zero when the external pointers go away, so neither destructor runs and both leak. Making one direction a weak_ptr, which does not count towards ownership, breaks the cycle. In the first version the destructors never print; in the second, both print.
cppstruct B; struct A { std::shared_ptr<B> b; ~A() { std::cout << "~A "; } }; struct B { std::weak_ptr<A> a; ~B() { std::cout << "~B "; } }; // weak back-link int main() { auto a = std::make_shared<A>(); a->b = std::make_shared<B>(); a->b->a = a; } // prints ~A ~B (with shared_ptr<A> in B: nothing printed, leak)6 How do you use a weak_ptr safely?
mediumA weak_ptr cannot be dereferenced directly. Call lock(), which returns a shared_ptr that is empty if the object has been destroyed, otherwise a new owning shared_ptr that keeps the object alive while you use it. expired() followed by lock() is racy in multithreaded code, so call lock() once and test the result. This is the pattern for caches, observers and parent back-pointers.
cppstd::weak_ptr<Widget> w = sp; if (auto s = w.lock()) { s->use(); } // alive for this scope else { /* already destroyed */ }7 Is shared_ptr thread-safe?
mediumPartly. The control block's reference counts are updated atomically, so different threads can copy and destroy different shared_ptr objects that share an object safely. But a single shared_ptr instance modified from two threads without synchronisation is a data race, and the pointed-to object has no protection at all. C++20 offers std::atomic<std::shared_ptr<T>> for concurrent updates of one pointer.
8 When is a custom deleter needed? Show an example.
mediumWhen the resource is not released by plain delete: a FILE* needing fclose, a C library handle, memory from malloc or an array. For unique_ptr the deleter type is part of the type, so a stateless function object adds no size; for shared_ptr it is type-erased in the control block and does not change the type.
cppauto closer = [](FILE* f) { if (f) fclose(f); }; std::unique_ptr<FILE, decltype(closer)> fp(fopen("a.txt", "r"), closer); std::shared_ptr<int> arr(new int[10], std::default_delete<int[]>());9 What are common mistakes with smart pointers?
mediumCreating two independent shared_ptr from the same raw pointer, giving two control blocks and a double delete; calling get() and then deleting or storing it past the owner; passing shared_ptr by value everywhere; using shared_ptr where unique_ptr would do; using std::move on a shared_ptr and then reading it; and creating a shared_ptr from "this", which should use enable_shared_from_this instead.
cppint* raw = new int(5); std::shared_ptr<int> a(raw); std::shared_ptr<int> b(raw); // double delete: two control blocks10 What is enable_shared_from_this and why is it needed?
hardIf a member function needs to hand out a shared_ptr to the current object, writing shared_ptr<T>(this) creates a second, unrelated control block and a double delete. A class deriving from std::enable_shared_from_this<T> stores a weak_ptr to the existing control block, and shared_from_this() returns a shared_ptr that shares it. The object must already be owned by a shared_ptr when you call it; before that it throws bad_weak_ptr (C++17).
11 What replaced auto_ptr and why?
easyauto_ptr was deprecated in C++11 and removed in C++17. Its "copy" silently transferred ownership, which made it unsafe in containers and algorithms that copy elements. unique_ptr replaced it with explicit move semantics: a copy is a compile error, and a move is visible in the code.
12 What is the size of a unique_ptr and a shared_ptr?
mediumWith the default deleter a unique_ptr is the size of one raw pointer (typically 8 bytes on 64-bit). A stateful custom deleter (a capturing lambda) adds its size. A shared_ptr is normally two pointers (16 bytes), plus the separately allocated control block. These are common but implementation defined, and a sizeof assertion on your platform confirms them.
13 How should functions take and return smart pointers?
mediumReturn unique_ptr from factories, which allows the caller to convert to shared_ptr if needed. Take unique_ptr by value to express "I take ownership". Take T& or T* for plain use without ownership, which keeps the function callable with any owner. Take shared_ptr by value only if the function shares ownership, for example storing it, and const shared_ptr& if it may or may not. Never take unique_ptr by reference unless you intend to reseat it.
Frequently asked questions
Should I always use shared_ptr instead of raw pointers?
No. Default to unique_ptr or plain objects, and use shared_ptr only when ownership really is shared. Shared ownership hides lifetimes and costs atomic operations.
What is the difference between reset and release on unique_ptr?
reset() destroys the managed object (optionally taking a new pointer) while release() gives up ownership and returns the raw pointer without destroying it.
Do smart pointers add run-time overhead?
unique_ptr has essentially none. shared_ptr adds atomic reference counting and a control block allocation.
Which questions follow smart pointers in interviews?
Move semantics, RAII and virtual destructors, covered in C++ interview questions.
Related guides
More in Programming Language Interview Questions by Language
- C++ STL Interview Questions
- C++ Templates Interview Questions
- C++ Virtual Functions Interview Questions
- Core Java Interview Questions for Freshers
- Entity Framework Core Interview Questions
- Java 8 Streams and Lambdas Interview Questions
- Java Coding Interview Questions
- Java Collections Interview Questions
- Java Concurrency Utilities Interview Questions
- Java Exception Handling Interview Questions
- Java Garbage Collection Interview Questions
- Java Generics Interview Questions