How to Use shared_ptr in C++: Simple Guide with Examples
In C++,
shared_ptr is a smart pointer that manages shared ownership of a dynamically allocated object. Multiple shared_ptr instances can point to the same object, and the object is deleted automatically when the last shared_ptr is destroyed or reset.Syntax
The basic syntax to create a shared_ptr is using std::make_shared<Type>(constructor_args) or by directly initializing it with a raw pointer. The shared_ptr keeps a reference count to know how many pointers share ownership.
- std::shared_ptr<Type> ptr; - declares a shared pointer.
- std::make_shared<Type>(args) - creates a shared_ptr managing a new object.
- ptr.use_count() - returns how many shared_ptr instances share ownership.
cpp
std::shared_ptr<Type> ptr = std::make_shared<Type>(constructor_args);
Example
This example shows how two shared_ptr instances share ownership of the same object. The object is automatically deleted when the last shared_ptr is destroyed.
cpp
#include <iostream> #include <memory> class MyClass { public: MyClass() { std::cout << "MyClass created\n"; } ~MyClass() { std::cout << "MyClass destroyed\n"; } void greet() { std::cout << "Hello from MyClass!\n"; } }; int main() { std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>(); { std::shared_ptr<MyClass> ptr2 = ptr1; // shared ownership std::cout << "Use count: " << ptr1.use_count() << "\n"; ptr2->greet(); } // ptr2 goes out of scope here std::cout << "Use count after ptr2 is out of scope: " << ptr1.use_count() << "\n"; return 0; }
Output
MyClass created
Use count: 2
Hello from MyClass!
Use count after ptr2 is out of scope: 1
MyClass destroyed
Common Pitfalls
Common mistakes when using shared_ptr include:
- Creating multiple
shared_ptrfrom the same raw pointer, causing double deletion. - Using
shared_ptrin cycles, which causes memory leaks because reference counts never reach zero. - Not using
std::make_shared, which is safer and more efficient than usingnewdirectly.
Always share ownership by copying existing shared_ptr instances, not by creating new ones from raw pointers.
cpp
#include <memory> int main() { // Wrong: two shared_ptr managing the same raw pointer int* raw = new int(10); std::shared_ptr<int> sp1(raw); std::shared_ptr<int> sp2(raw); // ERROR: double delete when both destruct // Right: share ownership by copying std::shared_ptr<int> sp3 = std::make_shared<int>(10); std::shared_ptr<int> sp4 = sp3; // both share ownership safely return 0; }
Quick Reference
| Feature | Description |
|---|---|
| std::make_shared | Creates a shared_ptr managing a new object safely and efficiently |
| shared_ptr | Copies shared_ptr to share ownership and increase reference count |
| ptr.use_count() | Returns the number of shared_ptr instances sharing ownership |
| ptr.reset() | Releases ownership, decreases reference count, deletes object if count is zero |
| Avoid multiple shared_ptr from same raw pointer | Prevents double deletion and undefined behavior |
Key Takeaways
Use std::make_shared to create shared_ptr safely and efficiently.
shared_ptr allows multiple owners; the object is deleted when the last owner is gone.
Never create multiple shared_ptr from the same raw pointer to avoid double deletion.
Use use_count() to check how many shared_ptr share ownership.
Beware of cyclic references as they cause memory leaks with shared_ptr.