Sub topic 2
How Arrays Sit in Memory
This chapter assumes you can write loops and functions, know introductory Big-O notation, and have a rough idea of what a memory address is. It covers the layout that gives arrays their speed and their limits.
One block, no gaps
An array stores its elements in contiguous memory. The elements occupy one unbroken block of addresses, one right after another, with nothing in between.
Suppose an array holds four 4-byte integers and starts at address 1000. The layout looks like this:
| Index | Address | Value |
|---|---|---|
| 0 | 1000 | 7 |
| 1 | 1004 | 3 |
| 2 | 1008 | 9 |
| 3 | 1012 | 5 |
The address where the array starts is called the base address. Each element takes the same number of bytes, called the element size. Because every slot is the same size and the slots touch, the position of any element follows from a simple formula:
address of element i = base address + i * element sizeFor index 2 in the table, that gives 1000 + 2 * 4 = 1008.
Seeing the layout in code
Python's array module stores raw numbers in one block, so you can inspect the layout directly. The buffer_info() method returns the base address and the number of elements.
from array import array
nums = array("i", [7, 3, 9, 5])
base, count = nums.buffer_info()
size = nums.itemsize
print("base address:", base)
print("element size:", size)
for i in range(count):
print(i, base + i * size, nums[i])The addresses printed in the second column step up by itemsize each time. The base address differs on every run, but the spacing does not. Here "i" is a C integer, usually 4 bytes.
Why this layout matters
The address formula needs one multiplication and one addition, no matter how large the array is. That is why reading or writing a[i] takes constant time, which Big-O notation writes as O(1). The next chapter builds on this point.
The same layout explains two costs you will meet later:
- Insertion and deletion in the middle. Since there must be no gaps, adding or removing an element means moving its neighbors to keep the block continuous.
- A fixed size. The block sits next to other data in memory. The program cannot assume the addresses just past the end are free, so the block cannot simply grow in place.
A note on Python lists
A Python list is not laid out like the array above. It stores a contiguous block of references (pointers), and each reference leads to an object elsewhere in memory. The block of references is contiguous, so indexing is still O(1). The objects they point to may be scattered, which matters for speed. Chapter 13 returns to this.
Common mistakes
- Assuming index 1 is the first element. With the formula above, the first element has offset 0, so it is at index 0.
- Assuming the elements of every Python list are adjacent. Only the references are adjacent, as described above.
- Assuming an array can grow in place. Growth requires finding a new block, which later chapters cover.
Exercise
An array of 8-byte values starts at address 5000. At what address does the element at index 6 sit, and why can the computer find it without looking at the other elements?
Show answer
The address is 5000 + 6 * 8 = 5048. The elements are contiguous and equal in size, so the address comes from the base address, the index and the element size with one multiplication and one addition. No other element has to be visited.
Exercise
A Python list holds three large strings. Which part of it is contiguous, and which part may not be?
Show answer
The list's block of references (pointers) is contiguous. The string objects the references point to may be anywhere in memory.