Pointers & Dynamic Memory — Addresses, Arrows, and Ownership

← Course home · ← Strings module · Unit 13/16 · Syllabus

Every variable you have ever made lives somewhere in memory. This unit lifts the floor: what memory really is, what an address is, and the two tools that use them — pointers (a variable that holds an address) and references (an alias for a variable). Then the grown-up part: dynamic memory, where your program asks the operating system for boxes at runtime with new and gives them back with delete — and the three classic ways to hurt yourself doing it (leaks, dangling pointers, invalid access), with the safety rules that prevent all three.

This is the unit students fear. It doesn’t need to be feared — it needs to be drawn. Every lesson here runs on memory diagrams, and the standing rule of this module is: if you haven’t drawn the boxes and arrows, you’re not working on the problem — you’re guessing.

In this module you will learn

Module map

Page What’s inside
Lesson 1 — Memory and addresses the numbered street, & and *, declaration, initialization, nullptr
Lesson 2 — References and functions pointer modification, references, refs vs pointers, out-parameters
Lesson 3 — Arrays and dynamic memory pointers with arrays, new/delete, dynamic arrays, leaks, dangling, ownership
Exercises 20 guided exercises with separated solutions (S1–S20)
Debugging 10 seeded memory bugs, with the safety-rule fix list
Tracing 10 memory-tracing exercises — draw the diagrams, answers separated
Challenges 10 challenges with separated solutions
Labs 4 labs that practice pointers safely — printed diagrams, no segmentation-fault roulette
Mini-project The Quiz Runner — a dynamically-sized records app

Try it yourself first

Same rule as every module: attempt 15 minutes before opening any solution — with one addition. For every pointer exercise, the first deliverable is not code. It is the diagram: one box per variable, the address written above each box, one arrow per pointer. The reference solution diagrams in tracing.md are drawn in exactly the style your own should be.

⚠️ Safety rules for this unit

Pointers are the first tool in this course that can crash the program outside your logic. These rules are non-negotiable, and every lab and exercise is built to stay inside them:

  1. Never dereference uninitialized. A pointer with garbage points somewhere random. Initialize at birth: int* p = nullptr; or int* p = &x;.
  2. Check for nullptr before dereferencing. A function that receives a pointer treats “might be null” as the normal case.
  3. Never use memory after delete. Set the pointer to nullptr on the same line — the Lesson 3 idiom.
  4. Every new gets exactly one delete. Two deletes corrupt; zero deletes leak. Count them in pairs.
  5. Prefer the tools that can’t fail this way. std::string, std::vector, and references solve most of these problems by design. new/delete appears in this course so you understand what those tools do for you — real projects reach for it rarely.

If you follow rule 5 everywhere except the exercises in this unit, you are not cheating — you are graduating.

Pacing

Unit 13 spans one week (2 sessions):

What comes next

References and pointers are the vocabulary of everything left: the Records module bundles related data into struct types, Unit 15 builds classes — where new/delete are wrapped inside constructors and destructors so you stop calling them — and Unit 16 is the capstone that assumes all of it.

Checklist