Modern C++: Rule of 5, Move Semantics, std::unique_ptr & std::shared_ptr
Comprehensive guide to Modern C++ memory safety: Move semantics (std::move, rvalue references &&), Rule of Five, zero-cost std::unique_ptr, and reference-counted std::shared_ptr.
Learning Objectives
- •Explain why C++ does not need a garbage collector or 'finally' blocks due to RAII.
- •Implement the complete Rule of Five for a custom dynamic vector/string class.
- •Differentiate lvalue references (&) from rvalue references (&&) and explain std::move.
- •Compare std::unique_ptr vs std::shared_ptr vs std::weak_ptr memory footprints and thread safety.
Essential Prerequisites
- •C++ classes, constructors, and destructors
- •Stack vs heap allocation
The Core Mental Model
Why This Exists
Older C++ ('C with classes') was notorious for memory leaks and segfaults. Modern C++ (C++11 through C++20) introduced move semantics and smart pointers, making C++ as memory-safe as modern managed languages while maintaining raw bare-metal C performance.
Beginner Foundation
In modern C++, you almost never write `new` or `delete`. Instead, you use `std::make_unique`. It automatically cleans up memory the exact moment your variable goes out of scope, even if an unexpected error or exception happens.
Micro Concepts Decomposition
The RAII Idiom: Deterministic Destruction
Resource Acquisition Is Initialization (RAII) ties resource lifespan (heap memory, file handles, database locks) to the lifetime of stack objects. When a stack object leaves scope, its destructor runs automatically, guaranteeing zero leaks even during exceptions.
The Rule of Five & Move Semantics
If a class manages raw resources, it must define 5 special member functions: 1) Destructor, 2) Copy Constructor, 3) Copy Assignment, 4) Move Constructor (T&&), 5) Move Assignment (T&&). Move semantics transfer ownership without deep copying.
std::unique_ptr: Exclusive Zero-Overhead Ownership
std::unique_ptr represents exclusive ownership of a heap resource. It cannot be copied, only moved (`std::move`). At runtime, std::unique_ptr has exactly zero overhead compared to a raw pointer — same memory footprint, same CPU cycles.
std::shared_ptr and std::weak_ptr: Reference Counting
std::shared_ptr provides shared ownership via an atomic control block containing use_count and weak_count. When use_count drops to 0, resource is deleted. std::weak_ptr observes without incrementing use_count, breaking cyclical references.
Hardware State Machine Architecture
Interactive Simulator
Python Object Identity (`is`), Equality (`==`) & PyObject Pointer Laboratory
Python pre-allocates an internal array of integer objects for values between -5 and 256 at interpreter startup. When you assign any integer in this range, Python points to the cached singleton PyObject rather than allocating a new object on heap!
End-to-End Execution Trace
Step-by-Step Code Execution (CPP)
Sandbox Terminal Ready
Click Run Code or press Ctrl+Enter to compile and execute.
Where Students Lose Marks
Active Assessment Quiz
No Practice Questions Configured
Questions for this topic are currently undergoing faculty review.