Shared pointer
std::shared_ptr<T> is a smart pointer with shared ownership. A control block holds a reference count. The last shared_ptr to die deletes the object. Copies increment the count; destruction decrements it.
Detailed Explanation
Sometimes two parts of the program both need to keep an object alive: a cache and a request handler, a scene graph node and a selection. unique_ptr cannot do that. shared_ptr can. The cost is a control block (refcount, weak count, deleter) and atomic increments on copy and destroy.
std::make_shared<T>(args) allocates the object and the control block together. That is one heap allocation instead of two (new plus a separate control block). Prefer it, except when you need a custom deleter or a weak_ptr cycle you are about to break with unique_ptr instead.
Aliasing constructor: shared_ptr<Member>(whole, &whole->member) shares ownership of whole but points at member. The member must not outlive the object; it cannot, because the object lives as long as any aliasing pointer.
Cycles: A holds shared_ptr<B> and B holds shared_ptr<A>. The counts never hit zero. Break the cycle with weak_ptr on one side, or do not share in a cycle (use unique_ptr plus raw observers).
Thread safety: the refcount is atomic. Increment and decrement from many threads are fine. Concurrent writes to the T are not. Concurrent assignment to the same shared_ptr object (the handle) is a data race unless you use std::atomic<shared_ptr<T>> (C++20). Different shared_ptr instances that own the same T can be copied on different threads.
shared_ptr<void> type-erases the pointer and still deletes the original type if the control block was created correctly (make_shared or shared_ptr<T>(new T)).
Do not create two shared_ptrs from the same raw pointer. Each creates a control block; you get a double delete. If you have a raw T* that is already owned, you cannot legally take shared ownership unless the object inherits enable_shared_from_this and was already created under a shared_ptr.
use_count() is a debug hint, not a synchronisation tool; it can change the next line.
Prefer unique_ptr unless you have measured the need to share. Shared ownership is harder to reason about than a single owner.
Code Examples
#include <iostream>
#include <memory>
int main() {
auto a = std::make_shared<int>(7);
auto b = a; // use_count == 2
std::cout << *b << ' ' << a.use_count() << '\n'; // prints: 7 2
b.reset();
std::cout << a.use_count() << '\n'; // prints: 1
}#include <memory>
#include <vector>
struct Node {
std::shared_ptr<Node> next;
};
int main() {
auto n = std::make_shared<Node>();
std::vector<std::shared_ptr<Node>> keep{n, n};
}Common Gotchas
Two `shared_ptr`s from one `new`
`shared_ptr<T> a(p); shared_ptr<T> b(p);` double-deletes. Clone from `a`, or use `enable_shared_from_this`.
Cycles
`weak_ptr` on the back edge, or rethink ownership.
`make_shared` and a custom deleter / weak leftover
the control block (and thus the allocation) lives as long as any `weak_ptr`, even after the object is destroyed. A huge object plus long-lived weaks keeps the memory.
Copying is an atomic increment
in a tight loop, pass `const shared_ptr<T>&` or a `T&` instead of copying the handle.