Sub topic 12
How a 2D Array Is Stored in Linear Memory
This chapter assumes you can index a nested list with grid[r][c], write nested loops, and use len(). It explains how a table of rows and columns fits into memory, which is a single line of cells.
Memory is one-dimensional
Computer memory is a long sequence of numbered slots. A 2D array has two indices, but the machine needs one position. Languages and libraries close this gap with a fixed rule that turns (row, column) into one flat index.
The most common rule is row-major order. It stores row 0 first, then row 1, and so on. Each row sits directly after the previous one.
The index formula
For a grid with cols columns, cell (r, c) lands at this flat position:
index = r * cols + cEach full row before row r takes cols slots, so r * cols skips them. Then c moves along the current row.
rows, cols = 2, 3
flat = [1, 2, 3, 4, 5, 6] # row-major storage of [[1, 2, 3], [4, 5, 6]]
r, c = 1, 2
index = r * cols + c
print(flat[index]) # 6
print(divmod(index, cols)) # (1, 2)divmod(index, cols) reverses the mapping. The quotient is the row and the remainder is the column. For index 5 with 3 columns, 5 // 3 = 1 and 5 % 3 = 2.
The formula uses cols, not rows. The number of rows only matters for knowing when the array ends. Mixing up the two is a common mistake, and it gives wrong results without raising an error whenever the index stays inside the flat list.
Column-major order
Column-major order stores column 0 first, then column 1, and so on. The formula becomes:
index = c * rows + rFortran, MATLAB, and R use column-major order. C, Java, and NumPy by default use row-major order. When you pass data between systems, check which rule each side expects. Otherwise you may read the transpose of your data.
| Layout | Formula | Cells next to each other in memory |
|---|---|---|
| Row-major | `r * cols + c` | Cells in the same row |
| Column-major | `c * rows + r` | Cells in the same column |
Why layout affects speed
Processors load memory in small blocks called cache lines. If your loop reads cells that sit next to each other, one load serves several reads. Walking along a row of a row-major array is fast. Walking down a column jumps cols slots each step, and it can load a new block each time.
The result is the same either way, but on large arrays the traversal that matches the layout can run noticeably faster.
Python lists are different
A Python nested list is not one block of values. The outer list holds references to inner lists, and each inner list holds references to its values. Rows can sit anywhere in memory and can have different lengths. The flat formula describes a conceptual or manual layout, like the flat list above, and fixed-size arrays in languages such as C. NumPy also stores its values in one block, which is one reason it handles large numeric grids well.
Common errors
- Using
rowsinstead ofcolsinr * cols + c. - Assuming a nested Python list is contiguous in memory.
- Reading column-major data as row-major, which silently transposes it.
Exercise
A 4 by 5 grid (4 rows, 5 columns) is stored row-major in a flat list. What is the flat index of the cell at row 2, column 3? What row and column does flat index 13 correspond to?
Show answer
Index = 2 * 5 + 3 = 13. For flat index 13, divmod(13, 5) = (2, 3), so row 2, column 3. Both questions describe the same cell.
Exercise
The same 4 by 5 grid is stored column-major instead. What is the flat index of row 2, column 3?
Show answer
Index = c rows + r = 3 4 + 2 = 14.