What computer memory actually looks like
Numbered boxes in a very long row, and why the numbering is the whole trick.
Everything in this course rests on one physical fact about computers, and it is easier than people usually make it sound. This lesson is that fact and nothing else. If you already know what an address is, you can skim it. If you do not, read it slowly, because the rest of the course quietly assumes it.
A very long corridor of numbered boxes
Memory is where a computer keeps whatever it is currently working with. Picture it as an extremely long corridor of identical small boxes, standing in a single row. Every box has a number painted on it, counting up from one end: box 0, box 1, box 2, and so on, for billions of boxes.
The number painted on a box is called its address. It is just a label saying where the box is in the row, in the same way a house number says where a house is on a street.
Each box is tiny. One box holds one byte, which is a single number somewhere between 0 and 255. That is all a byte is. It is not enough to hold much, so anything larger uses several boxes side by side. A common size for a whole number in a program is four bytes, which is four boxes in a row.
address: 200 201 202 203 204 205 206 207
+----+----+----+----+ +----+----+----+----+
byte: | 42 | 0 | 0 | 0 | | 7 | 0 | 0 | 0 |
+----+----+----+----+ +----+----+----+----+
\_________________/ \_________________/
the number 42 the number 7
starts at 200 starts at 204All four boxes together hold the one number. Since 42 is small it fits in the first box and the other three are left at zero, but they still belong to this number. What those extra three boxes buy is worth counting, because it is the only reason for using four of them.
One box has 256 possible values. Two boxes side by side have 256 times 256, which is 65,536. Four boxes have 256 multiplied by itself four times, which is a little over four billion. That is the whole trick: the boxes multiply rather than add. Which box holds which part of the number varies between machines, and it does not matter here.
When we say a value is at address 200, we mean the run of boxes holding it begins at box 200. Since we agreed each number here takes four boxes, the next one along begins at 204, then 208, then 212.
The fact everything else depends on
Here is the important part. The computer does not have to walk down the corridor. Given a box number, it can go straight to that box, and doing so takes the same amount of work no matter which number you name.
Fetching box 7 and fetching box 7,000,000 take the same work. There is no walking past the boxes in between, no counting, no searching. You could think of it as a corridor where you can step directly into any room if you know the room number.
Asking for space: allocation
A program does not get to scribble in any box it fancies. Other programs and other parts of the same program are using memory too. So when a program needs somewhere to put things, it asks for space. That request is called allocating memory: you say how many boxes you need, and you are told the address where your run of free boxes begins.
If you ask for space for five four-byte numbers, you are asking for twenty boxes, and you might be told they start at address 200. Boxes 200 to 219 are now yours until you say you are finished with them, which is called releasing or freeing them.
You are given exactly the boxes you asked for and no more. The boxes immediately after yours, 220 onwards, may already belong to another part of the program, so you cannot quietly take a few extra later on. If it turns out you need a longer run, you have to ask for a whole new one somewhere else and move your things into it. That small fact has a lesson of its own later in this section.
Why indices start at zero
This is the promised explanation. You have a run of boxes starting at address 200, each item taking four boxes. Where does each item begin?
- The first item begins at 200, which is 200 plus 0 fours.
- The second begins at 204, which is 200 plus 1 four.
- The third begins at 208, which is 200 plus 2 fours.
- The fourth begins at 212, which is 200 plus 3 fours.
The number in bold is the index. It is not counting items, it is counting how far along you have to go from the start. The first item is zero steps from the start, so its index is 0. Numbering from zero is not awkwardness for its own sake. It is the number that makes the arithmetic come out right.
A run of memory starts at address 500 and holds items that are four bytes each. Which boxes does the item at index 2 occupy?
Boxes 508, 509, 510 and 511. Index 2 means two items along from the start, and each item is four boxes, so it begins at 500 plus 2 times 4, which is 508. It then occupies four boxes from there.