Smart Pointers, Memory Ownership, and Perfect Forwarding
Eliminate raw pointer bugs using std::unique_ptr, std::shared_ptr control blocks, std::weak_ptr cycle breaking, and universal reference perfect forwarding.
RAII and the Rule of Five
Command of std::unique_ptr, std::shared_ptr control blocks, std::weak_ptr cycle breaking, and perfect forwarding.
Read in order, or jump to what you need.
Every lesson builds on the one before it. Open any lesson to read the explanation, run the examples, and try the practice challenges.
- 04.01Exclusive Ownership: std::unique_ptr, make_unique, and Custom Deleters4 min
Master exclusive resource ownership with std::unique_ptr: zero runtime overhead, move-only semantics, custom deleter function pointers, and std::make_unique.
- 04.02Shared Ownership: std::shared_ptr and the Anatomy of Control Blocks4 min
Dissect shared ownership semantics: reference count increments/decrements, thread-safe atomic counters, control block heap layout, and make_shared vs explicit allocation.
- 04.03Cyclic References, Memory Leaks, and std::weak_ptr4 min
Diagnose circular reference memory leaks in graph and observer patterns, and break reference cycles using std::weak_ptr and the lock() promotion idiom.
- 04.04Forwarding References, Reference Collapsing, and std::forward4 min
Master template generic forwarding: universal (forwarding) references (T&&), reference collapsing rules (& + &&), and perfect argument forwarding via std::forward.
Ready to move on?
Implement custom deleters and write generic factory functions utilizing std::forward.
Open the module self-check