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::uniqueptrfor exclusive ownership.
-
Use
std::sharedptrfor shared ownership.
-
Understand
std::weakptrand cyclic references.
-
Safely allocate memory using
std::makeunique.
3. The Problem with Raw Pointers
cpp
4. std::uniqueptr (Exclusive Ownership)
A uniqueptr owns the memory exclusively. No two uniqueptrs can point to the same memory. When the uniqueptr goes out of scope, it automatically calls delete for you.
*(Requires #include <memory>)*
cpp
5. std::sharedptr (Shared Ownership)
If multiple parts of your program need to point to the exact same data, use sharedptr. 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::weakptr (Breaking Cycles)
A weakptr is an observer. It points to memory managed by a sharedptr, but it does NOT increase the reference count.
-
Why use it? To prevent Cyclic References. If Object A has a
sharedptrto Object B, and Object B has asharedptrto Object A, their reference counts will never reach 0. They will keep each other alive forever (Memory Leak!). Using aweakptrfor one of those links solves this.
7. Modern C++ Best Practices
-
Rule #1: Never use
newordeletein modern C++ applications.
-
Rule #2: Default to
std::uniqueptrfor everything.
-
Rule #3: Only upgrade to
std::sharedptrif 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); uniqueptr<int> p(raw);This is dangerous if someone else deletesrawmanually. Always usemakeuniqueormakeshared.
-
Copying a uniqueptr: 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::uniqueptrto an array of 5 integers.
-
2.
Demonstrate how a
std::sharedptrtracks its reference count by printing.usecount()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 uniqueptr?
Question 5
How does a sharedptr know when to delete the memory?
Question 6
What function is best practice for creating a uniqueptr?
Question 7
Which smart pointer is used to observe a sharedptr without increasing its reference count?
Question 8
What problem does weakptr 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
sharedptr, and how to fix it.
-
Q: Why is
std::makesharedpreferred overstd::sharedptr<T>(new T())? (Hint: Performance and single memory allocation).
13. Summary
Smart Pointers (uniqueptr, sharedptr, weakptr) 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.