foundation Estimated learning time: 12 h

P-1.2 Memory, Addresses and What a Variable Really Is

You can describe memory as numbered storage, explain what a variable is in terms of an address and a size, and say what the stack and the heap are for.

A variable is usually taught as a box with a name, which is close enough to write a first program and wrong enough to make pointers, references and memory bugs incomprehensible later. Here it becomes what it is: a name your language keeps for a location and a size. The stack and the heap are introduced now because almost every later confusion about lifetimes, copying and leaks traces back to not knowing which one something lives in.

Work through these

  • Memory as one long row of numbered bytes

    Main memory is a very long sequence of bytes, each with a number called its address. Nothing in it is labelled, and every name in your program is a convenience your language maintains on top of that plain numbering.

  • What a variable is: a name, an address, a size and a way of reading the bits

    Storing a value means writing bytes at some address and remembering how many and how to interpret them. The same eight bytes are a number or a piece of text depending only on the type your program says they are.

  • The stack: automatic storage that disappears when a function returns

    Local values live in a region that grows and shrinks as functions are called and return, which is why they cost almost nothing and why holding on to one after its function has returned is a bug.

  • The heap: storage you ask for, and must give back

    When a value has to outlive the function that made it, or its size is unknown until the program runs, it goes in a region you request explicitly. Everything about memory leaks and use-after-free lives here.

    CS50x: Introduction to Computer Science · Course
  • Copying a value against copying a reference to it

    Passing a small number usually copies the number, and passing a large structure usually copies only its address. Which one your language does decides whether a change inside a function is visible outside it.

Sign in to keep your progress.

Free resources

We haven't checked most of these for screen reader use yet.

Links last checked 31 Aug 2026.

Stuck here?

Ask a mentor. A real person answers, and they can see exactly which topic you're on. Usually within a couple of working days.

Checking your session…

Topics shown in module order.