← All Lessons Lesson 5 / 68
Lesson 5

How Arrays Really Work in Memory

How Arrays Really Work in Memory

You already know what an array is and how it helps you store lots of data together. Now let's open the hood and see *why* arrays are so fast. The secret is in how they sit inside the computer's memory.

Memory is just a long row of boxes

Think of your computer's memory (RAM) as a very long shelf made of tiny boxes lined up one after another. Each box holds exactly 1 byte of data (a byte is 8 bits).

Every box has a number that tells you its position on the shelf. This number is called its address. Addresses are counted from the start of the shelf, beginning at 0:

  • The first box is at address 0
  • The next box is at address 1
  • Then 2, 3, 4, and so on

So an address is nothing fancy — it is simply "how far this box is from the very beginning." High-level languages hide all of this from you, but deep down, this is exactly how data is stored.

An array is a connected block of boxes

When you make an array, the computer reserves a contiguous stretch of these boxes — meaning the boxes are right next to each other, with no gaps. The array stores items of one single type, so every item takes up the same amount of space (the size of that data type).

Because all items are the same size, the total space an array uses is just:

> total size = (size of one item) × (number of items)

For example, imagine an array of 5 values where each value needs 4 bytes, and the array starts at address 2:

| value | index | starting address |

|-------|-------|------------------|

| value1 | 0 | 2 |

| value2 | 1 | 6 |

| value3 | 2 | 10 |

| value4 | 3 | 14 |

| value5 | 4 | 18 |

Base address: where the array begins

The address of the very first box of an array is called its base address. In the example above, the base address is 2. This one number, together with the index, is all the computer needs to find any item.

The formula to find any item

Here is the magic. To jump straight to the item at any index, the computer uses this simple formula:

address of item at index = base address + (size of datatype * index)

Let's try it with our example (base address 2, each item 4 bytes):

  • Item at index 0: 2 + (4 * 0) = 2
  • Item at index 2: 2 + (4 * 2) = 10
  • Item at index 4: 2 + (4 * 4) = 18

Notice that the computer does not walk through the array one box at a time looking for index 4. It does one multiplication and one addition, and lands on the exact box instantly. This is why reading any element of an array is so fast — it takes the same tiny amount of work no matter how big the array is.

Why indices start at 0

Now the famous question finally makes sense. Look at the formula again: the index is added to the base address. To get the *first* element, you want to land right on the base address itself — so you must add 0.

That is exactly why array indices start at 0 and not 1. Index 0 means "zero steps away from the start," which is the first item. Index 1 means "one item's worth of space away," which is the second item, and so on.

How Arrays Really Work in Memory
Diagram — click to zoom (scroll / drag to pan)