Topic 85 of 91 11. Additional C++ Topics Added Interactive Tutorial

Smart Pointers in C++: unique_ptr, shared_ptr, and weak_ptr

8 mins read Last updated: 26 Sep 2026 19,645 views

Direct Answer & Core Concept

Smart pointers in `` are RAII template classes that manage dynamic heap objects. std::unique_ptr provides exclusive zero-overhead ownership (cannot be copied, only moved). std::shared_ptr provides reference-counted shared ownership. std::weak_ptr provides a non-owning observer to break circular reference cycles in shared_ptr hierarchies.

Detailed Explanation & Mechanics

1. Smart Pointer Comparison:

Architecture Diagram
┌─────────────────┬──────────────────────┬──────────────────────┬─────────────────────────┐
│ Smart Pointer   │ Ownership Model      │ Overhead             │ Copy / Move Ability     │
├─────────────────┼──────────────────────┼──────────────────────┼─────────────────────────┤
│ std::unique_ptr │ Exclusive Single     │ 0 bytes (Same as raw)│ Move-Only (No copy)     │
│ std::shared_ptr │ Shared Ref-Counted   │ 16-24 bytes (Control)│ Copyable & Moveable     │
│ std::weak_ptr   │ Non-owning Observer  │ Control Block Pointer│ Breaks circular cycles  │
└─────────────────┴──────────────────────┴──────────────────────┴─────────────────────────┘

2. Circular Reference Prevention:

Two shared_ptr objects referencing each other create a reference count that never drops to 0. Replacing one side with std::weak_ptr resolves the leak.

Runnable Code Example: Smart Pointers in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <memory>
#include <string>

class Resource {
public:
    std::string name;
    explicit Resource(std::string n) : name(std::move(n)) {
        std::cout << "[Constructed] Resource: " << name << "\n";
    }
    ~Resource() {
        std::cout << "[Destroyed] Resource: " << name << "\n";
    }
};

int main() {
    // 1. std::unique_ptr (Exclusive Ownership)
    std::cout << "--- unique_ptr Demo ---\n";
    {
        auto uPtr = std::make_unique<Resource>("Unique-01");
        // auto uCopy = uPtr; // COMPILE ERROR: Cannot copy unique_ptr!
        auto uMoved = std::move(uPtr); // Ownership transferred
    } // Destroyed here

    // 2. std::shared_ptr and std::weak_ptr
    std::cout << "\n--- shared_ptr & weak_ptr Demo ---\n";
    std::weak_ptr<Resource> observer;
    {
        auto sPtr1 = std::make_shared<Resource>("Shared-02");
        observer = sPtr1;
        std::cout << "Shared use count: " << sPtr1.use_count() << "\n";
        {
            auto sPtr2 = sPtr1; // Ref count = 2
            std::cout << "Shared use count in inner scope: " << sPtr1.use_count() << "\n";
        } // sPtr2 leaves scope, Ref count = 1
        std::cout << "Shared use count after inner scope: " << sPtr1.use_count() << "\n";
    } // sPtr1 leaves scope, Ref count = 0 -> Resource destroyed

    std::cout << "Is observed object expired? " << (observer.expired() ? "YES" : "NO") << "\n";
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: Why should you use `std::make_unique` and `std::make_shared` instead of `new`?

A: `std::make_unique` is exception-safe and prevents leaks during argument evaluation. `std::make_shared` allocates the control block and object in a single contiguous memory allocation, improving cache locality.
Q

Q: How does `std::weak_ptr` access its underlying object?

A: It must be converted to a `std::shared_ptr` using `.lock()` to verify the object still exists before dereferencing.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Prefer std::unique_ptr by default; upgrade to std::shared_ptr only when multiple entities genuinely share ownership.
• Always use std::make_unique and std::make_shared instead of raw new.
• Use std::weak_ptr for observer patterns and tree parent/sibling pointers to prevent circular dependency memory leaks.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.