Skip to content
ByteStepStart with recursion

Memory, addresses and pointers

Free, no account

Memory is a single very long row of numbered boxes. Each box holds a number. The number of a box is its memory address, and that is the whole of the model: there is no hierarchy, no names, and nothing inside the machine that knows a box is called a.

Below, a short program runs a line at a time and memory is drawn after every one. Watch where each new value lands: one box after the last, 8 bytes along, every time.

Then press pointer. One line changes, and the number put into the new box is not a quantity but an address. That is the entire idea, and everything difficult about pointers is downstream of it.

1let a = 42
2// ...more variables...
3
4let copy = a // takes the value
5copy = 99 // writes to copy
6
7print(a) // still 42

Memory

one box per value, with the address it lives at

Memory · 56 byteslow address
0x7FFD0078 · limit0 named · 0 bytes

Memory is a row of numbered boxes. Every box has an address, and every value here takes 8 bytes of it.

1/8
Detail
8 steps

Names against addresses, why a copy is a second box, and what a pointer actually holds.

Names are for you, addresses are for the machine

When the program says a, the machine does not look up a name. The name existed while the code was being translated, and what came out the other side was an address. By the time anything runs, the box is only a number and its contents are only a number.

Each value here takes 8 bytes, and each new one goes in the next box along. Drag the slider and watch the addresses climb by 8 each time. That regularity is not a detail; the next lesson is built entirely on it.

A copy is a second box

Run copy to the end. Two boxes now hold 99 and 42, and nothing connects them. Writing to one cannot possibly affect the other, because they are different boxes and always were. The value was copied at the moment of assignment and the two have had separate lives ever since.

This is what “passing by value” means, and it is why a function that takes a number and changes it changes nothing anybody else can see.

A pointer is a box holding an address

Now run pointer. The new box holds the first box's address, written in the same notation as the addresses down the side, so you can match them by eye.

The next line writes through it. To follow a pointer, the machine reads the number in the box, treats that number as an address, and goes there. That is all dereference means. The value 99 lands in a, and the pointer itself is unchanged, because nothing was written to the pointer.

At the end a reads back as 99, having never been assigned to directly. After the copy it is still 42. Same program, same values, one line different.

Notice what is not drawn

There is no arrow. Every textbook draws one, curving from the pointer to the thing it points at, and that arrow is probably the single biggest reason pointers feel like magic: it makes the connection look like a thing that exists in the machine.

It does not. There is a box with a number in it, and the number happens to match another box's address. Nothing marks that box as special, nothing checks it, and if the number is wrong the machine will follow it anyway. Press null to watch that happen: the box holds zero, zero is not the address of anything, and the program stops.

Know someone stuck on this? Send it to them.

A classmate, a study group, someone learning this on their own at midnight. The link opens the lesson set up exactly as you have it, the same function and the same number, and runs from the first step so they watch the whole thing build rather than landing in the middle of it.

Next in this course

Arrays: why the index is free

Same size, one after another. That arrangement is what lets a position be calculated instead of searched for.

Read it next →