# Shared Pointer

As we saw, unique pointer are one-to-one mapped. Meaning, one pointer variable can only stored by one `unique_prt` and it cannot be referenced by any other unique pointer. A unique pointer will free the referenced memory as soon as this `unique_ptr` goes out of scope.

A `shared_ptr` is different from that, here you can create multiple wrappers that are refer to same pointer, memory will be freed once all the `shared_ptr` goes out of scope.

Shared pointers are a type of smart pointer introduced in C++11 that provide automatic memory management through reference counting. Here's a concise overview:

Shared pointers (`std::shared_ptr`) allow multiple pointers to share ownership of a dynamically allocated object. Key points:

1. Automatic memory management: The object is automatically deleted when the last shared pointer owning it is destroyed.
2. Reference counting: Internally maintains a count of how many shared pointers are pointing to the object.
3. Thread-safe reference count: The reference count is incremented and decremented in a thread-safe manner.
4. Overhead: Slight performance overhead due to reference counting.
5. Cyclic references: Can lead to memory leaks if not handled properly (use weak\_ptr to break cycles).

```cpp
#include <memory>
#include <iostream>

class MyClass {
public:
    MyClass() { std::cout << "MyClass constructed\n"; }
    ~MyClass() { std::cout << "MyClass destructed\n"; }
};

int main() {
    {
        std::shared_ptr<MyClass> ptr1 = std::make_shared<MyClass>();
        {
            std::shared_ptr<MyClass> ptr2 = ptr1; // Share ownership
            std::cout << "Count: " << ptr1.use_count() << std::endl; // Prints 2
        }
        std::cout << "Count: " << ptr1.use_count() << std::endl; // Prints 1
    }
    // MyClass object is automatically deleted here
    return 0;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://notes.tejpratapsingh.com/_/cpp/memory/pointers/smart-pointers/shared-pointer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
