CHAPTER 25
Beginner
Smart Pointers and Modern C++
Updated: May 17, 2026
5 min read
# CHAPTER 25
Smart Pointers and Modern C++
1. Introduction
For decades, the biggest criticism of C++ was Memory Leaks. If a programmer usednew but forgot delete (or if an exception caused the program to skip the delete line), memory was lost forever. In 2011, Modern C++ (C++11) introduced Smart Pointers, practically eliminating the need for raw pointers and manual memory management.
2. Learning Objectives
By the end of this chapter, you will be able to:-
Explain why raw pointers (
*) are dangerous.
-
Use
std::unique_ptrfor exclusive ownership.
-
Use
std::shared_ptrfor shared ownership.
-
Understand
std::weak_ptrand cyclic references.
-
Safely allocate memory using
std::make_unique.
3. The Problem with Raw Pointers
cpp
4. std::unique_ptr (Exclusive Ownership)
A unique_ptr owns the memory exclusively. No two unique_ptrs can point to the same memory. When the unique_ptr goes out of scope, it automatically calls delete for you.
*(Requires #include <memory>)*
cpp
5. std::shared_ptr (Shared Ownership)
If multiple parts of your program need to point to the exact same data, use shared_ptr. It keeps a Reference Count. Every time you copy the pointer, the count goes up. Every time a pointer is destroyed, the count goes down. When the count hits 0, it calls delete.
cpp
6. std::weak_ptr (Breaking Cycles)
A weak_ptr is an observer. It points to memory managed by a shared_ptr, but it does NOT increase the reference count.
-
Why use it? To prevent Cyclic References. If Object A has a
shared_ptrto Object B, and Object B has ashared_ptrto Object A, their reference counts will never reach 0. They will keep each other alive forever (Memory Leak!). Using aweak_ptrfor one of those links solves this.
7. Modern C++ Best Practices
-
Rule #1: Never use
newordeletein modern C++ applications.
-
Rule #2: Default to
std::unique_ptrfor everything.
-
Rule #3: Only upgrade to
std::shared_ptrif you explicitly need shared ownership.
8. Memory-Level Explanation
Smart pointers are essentially small wrapper classes allocated on the Stack. Inside the wrapper is the raw pointer to the Heap memory. Because the wrapper is on the Stack, its Destructor is *guaranteed* to run when it goes out of scope (even during an Exception!). Inside that Destructor is the code that automatically callsdelete on the raw Heap pointer.
9. Common Mistakes
-
Mixing raw pointers with smart pointers:
int* raw = new int(5); unique_ptr<int> p(raw);This is dangerous if someone else deletesrawmanually. Always usemake_uniqueormake_shared.
-
Copying a unique_ptr: The compiler will prevent this. If you must transfer ownership, you must use
std::move(ptr).
10. Exercises
-
1.
Write a program using a
std::unique_ptrto an array of 5 integers.
-
2.
Demonstrate how a
std::shared_ptrtracks its reference count by printing.use_count()before and after a new scope block{ }.
11. MCQ Quiz with Answers
Question 1
What is the main purpose of Smart Pointers?
Question 2
Which library is required to use smart pointers?
Question 3
Which smart pointer allows only one owner at a time?
Question 4
Can you copy a unique_ptr?
Question 5
How does a shared_ptr know when to delete the memory?
Question 6
What function is best practice for creating a unique_ptr?
Question 7
Which smart pointer is used to observe a shared_ptr without increasing its reference count?
Question 8
What problem does weak_ptr solve?
Q10. Is C++ natively garbage-collected like Java or C#? a) Yes b) No, it uses determinism (RAII) and smart pointers instead Answer: b) No, it uses determinism (RAII) and smart pointers instead
12. Interview Questions
- Q: Explain RAII (Resource Acquisition Is Initialization) in C++ and how smart pointers utilize it.
-
Q: Describe a scenario where a cyclic reference causes a memory leak with
shared_ptr, and how to fix it.
-
Q: Why is
std::make_sharedpreferred overstd::shared_ptr<T>(new T())? (Hint: Performance and single memory allocation).
13. Summary
Smart Pointers (unique_ptr, shared_ptr, weak_ptr) modernize C++ by wrapping dangerous raw pointers in safe, stack-allocated classes. They automatically clean up memory when they go out of scope, eliminating memory leaks and making C++ as safe as garbage-collected languages, but with much better performance.