0
0
CppConceptBeginner · 3 min read

What is shared_ptr in C++: Simple Explanation and Usage

shared_ptr is a smart pointer in C++ that manages shared ownership of a dynamically allocated object. It keeps track of how many shared_ptr instances point to the same object and automatically deletes the object when the last pointer is destroyed or reset.
⚙️

How It Works

Imagine you have a book that several friends want to read. Instead of each friend buying their own copy, they share one copy and keep track of how many people currently have it. When the last friend returns the book, it gets put away safely. shared_ptr works similarly for memory in C++.

When you create a shared_ptr, it holds a pointer to an object and a counter that tracks how many shared_ptr instances share ownership of that object. Every time you copy a shared_ptr, the counter increases. When a shared_ptr is destroyed or reset, the counter decreases. Once the counter reaches zero, meaning no one owns the object anymore, the object is automatically deleted.

This mechanism helps prevent memory leaks and dangling pointers by ensuring the object lives as long as it is needed and no longer.

💻

Example

This example shows how shared_ptr shares ownership of an integer and deletes it automatically when no pointers remain.

cpp
#include <iostream>
#include <memory>

int main() {
    std::shared_ptr<int> ptr1 = std::make_shared<int>(42); // create shared_ptr
    std::cout << "Value: " << *ptr1 << std::endl;
    std::cout << "Use count: " << ptr1.use_count() << std::endl;

    {
        std::shared_ptr<int> ptr2 = ptr1; // copy shared_ptr
        std::cout << "Inside block, use count: " << ptr1.use_count() << std::endl;
    } // ptr2 destroyed here, count decreases

    std::cout << "After block, use count: " << ptr1.use_count() << std::endl;
    return 0;
}
Output
Value: 42 Use count: 1 Inside block, use count: 2 After block, use count: 1
🎯

When to Use

Use shared_ptr when multiple parts of your program need to share ownership of a resource, such as a dynamically allocated object, and you want automatic cleanup when no one uses it anymore.

For example, in a graphics program, multiple objects might share access to a texture. Using shared_ptr ensures the texture stays alive as long as needed and is deleted when no longer used.

However, avoid shared_ptr if ownership is clear and single, where unique_ptr is better for performance and clarity.

Key Points

  • Automatic memory management: deletes object when last owner goes away.
  • Reference counting: tracks how many shared_ptr instances share ownership.
  • Safe sharing: multiple pointers can safely share the same object.
  • Overhead: has some performance cost due to reference counting.
  • Use cases: ideal when ownership is shared and lifetime is uncertain.

Key Takeaways

shared_ptr manages shared ownership of a dynamically allocated object with automatic cleanup.
It uses reference counting to track how many pointers share the object.
The object is deleted when the last shared_ptr is destroyed or reset.
Use shared_ptr when multiple parts of code need to share ownership safely.
Avoid shared_ptr if ownership is unique; prefer unique_ptr then.