Back to library

🧠C++ Memory Management Basics

Trade segfaults and double-frees for ownership you can reason about. By drop 14 you'll ship a small RAII resource wrapper that handles exceptions without leaking — and you'll know exactly why unique_ptr is the default and shared_ptr the exception.

Foundations14 drops~2-week path · 5–8 min/daytechnology

Phase 1The Cost of Manual Memory

Stack, heap, and the real cost of manual memory

4 drops
  1. Your variables live in two completely different worlds

    6 min

    Your variables live in two completely different worlds

  2. Every `new` is a promise, and you owe a `delete`

    6 min

    Every `new` is a promise, and you owe a `delete`

  3. The bug isn't the leak — it's the path you didn't see

    7 min

    The bug isn't the leak — it's the path you didn't see

  4. Wrap the resource in an object and let scope do the work

    7 min

    Wrap the resource in an object and let scope do the work

Phase 2Smart Pointers in Practice

Swap raw pointers for unique_ptr and shared_ptr

5 drops
  1. One owner, no exceptions — that's `unique_ptr`

    7 min

    One owner, no exceptions — that's `unique_ptr`

  2. Shared ownership has a price tag — read it before you swipe

    7 min

    Shared ownership has a price tag — read it before you swipe

  3. Break the cycle with a pointer that doesn't own

    7 min

    Break the cycle with a pointer that doesn't own

  4. Smart pointers don't only manage `delete` — they manage anything

    8 min

    Smart pointers don't only manage `delete` — they manage anything

  5. Three smart pointer mistakes that look right and aren't

    7 min

    Three smart pointer mistakes that look right and aren't

Phase 3RAII, Moves, and Exception Safety

Wire RAII to destructors, moves, and exception safety

4 drops
  1. Your service leaks file handles. The `new` is fine. What's wrong?

    7 min

    Your service leaks file handles. The `new` is fine. What's wrong?

  2. You inherit a function returning `vector<Big>` by value. The reviewer says it's slow. Is it?

    8 min

    You inherit a function returning `vector<Big>` by value. The reviewer says it's slow. Is it?

  3. Your wrapper class works in tests and double-frees in production. Why?

    8 min

    Your wrapper class works in tests and double-frees in production. Why?

  4. Code review asks for 'strong exception safety.' What does that even mean?

    8 min

    Code review asks for 'strong exception safety.' What does that even mean?

Phase 4Build an Exception-Safe Wrapper

Build an exception-safe resource wrapper from scratch

1 drop
  1. Build a `FileBuffer` class that handles every exception path correctly

    25 min

    Build a `FileBuffer` class that handles every exception path correctly

Frequently asked questions

When should I use unique_ptr versus shared_ptr in C++?
This is covered in the “C++ Memory Management Basics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What is RAII and why does C++ depend on it?
This is covered in the “C++ Memory Management Basics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
Why is `new`/`delete` discouraged in modern C++?
This is covered in the “C++ Memory Management Basics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
How do move semantics interact with smart pointers?
This is covered in the “C++ Memory Management Basics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.
What makes a class exception-safe in C++?
This is covered in the “C++ Memory Management Basics” learning path. Start with daily 5-minute micro-lessons that build from fundamentals to hands-on application.