C++ · Advanced level

Advanced level · Module 2 of 6 · 24 guided hours

Modern Memory Management

Understand stack and heap memory, express ownership with smart pointers, and make types efficient to move.

Learning objectives

  • Explain stack and heap allocation and the dangers of manual memory management
  • Express ownership with unique_ptr, shared_ptr and weak_ptr
  • Implement and use move semantics and the rule of five

Topics

  • stack vs heap
  • new/delete pitfalls
  • std::unique_ptr and make_unique
  • std::shared_ptr and weak_ptr
  • rvalue references and std::move
  • rule of five

Lessons

  1. Lesson 1 Stack, Heap and Manual Memory

    See where objects live, why manual new and delete are error-prone, and what goes wrong in practice.

    stack allocationheap allocationmemory leakdouble delete

  2. Lesson 2 unique_ptr: Single Ownership

    Express exclusive ownership with std::unique_ptr, transfer it with std::move and use it in containers.

    std::unique_ptrstd::make_uniqueownership transfernon-owning raw pointer or reference

  3. Lesson 3 shared_ptr and weak_ptr

    Use shared ownership only when it is genuinely needed, and break reference cycles with weak_ptr.

    std::shared_ptrreference countstd::weak_ptrreference cycle

  4. Lesson 4 Move Semantics and the Rule of Five

    Make types cheap to move, understand rvalue references and know when to write the five special member functions.

    rvalue reference &&std::movemove constructor / move assignmentrule of five

Practical activity

Refactor a provided program that leaks memory and double-deletes into one with clear ownership using smart pointers, verified with AddressSanitizer.

Expected outcomes

  • Leak-free code with explicit ownership
  • Efficient move-aware types

Assessment

Refactoring practical marked by sanitizer results and an ownership review of every pointer.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.