algoritmauzmanı
Sign in
TREN
2.3Multidimensional Arrays
Lesson 2.3.2

Row-major and column-major order

Memory is one-dimensional: bytes are numbered in a single line. A two-dimensional array, however, has rows and columns. The compiler lays the matrix out along that single line in a fixed order. In this lesson you learn the two layouts and the formula that gives the address of an element. The formula comes up often in exams.

What you will learn
  • How and column-major order differ, and which language uses which
  • Computing the address of A[i][j] from the base address, the indices and the element size
  • How the formula changes when the lower bounds are not 0
7 minTime O(1)Memory O(1)C · Java · Python

1Idea

The 12 elements of a 3×4 matrix go into 12 slots in memory. One of two orders decides which element goes into which slot.

In row-major order, all elements of row 0 come first, then those of row 1. Before A[i][j] there are i full rows, that is i × num_cols elements, and then j elements in the same row:

address(A[i][j]) = base + (i × num_cols + j) × element_size

In column-major order, all elements of column 0 come first, then those of column 1. This time there are j full columns in front, and i elements in the same column:

address(A[i][j]) = base + (j × num_rows + i) × element_size

row 0row 1row 2memory1[0][0]2[0][1]3[0][2]4[0][3]5[1][0]6[1][1]7[1][2]8[1][3]9[2][0]10[2][1]11[2][2]12[2][3]110005100491008210126101610102031024710281110324103681040121044
The same matrix in column-major order. Memory holds column 0 first (1, 5, 9), then column 1. Here A[1][2] = 7 is at index 2 × 3 + 1 = 7, address 1028.

Which language uses which?

  • C and C++: row-major. int A[5][8] is one contiguous block and the formula applies exactly.
  • Fortran, MATLAB, R: column-major.
  • Java: int[][] is an array of arrays. Each row is a separate array and contiguous on its own, but the rows do not have to sit next to each other in memory. The formula only holds within a row.
  • Python: in a list of lists each row is a separate list, and it holds rather than the elements themselves. NumPy arrays are row-major by default (order='C') and can be made column-major (order='F').

Worked example

Question: the array int A[5][8] is stored in row-major order starting at address 1000, and an int takes 4 bytes. What is the address of A[3][6]?

  1. There are 8 columns. Before A[3][6] there are 3 full rows: 3 × 8 = 24 elements.
  2. In the same row there are 6 elements in front: 24 + 6 = 30.
  3. Convert to bytes: 30 × 4 = 120.
  4. Add the base address: 1000 + 120 = 1120.

If the same array were column-major, the number of rows (5) would be used: 1000 + (6 × 5 + 3) × 4 = 1000 + 132 = 1132.

If a lower bound is not 0, subtract it from the index first. For A[1..5][1..8] (rows 1 to 5, columns 1 to 8): address(A[i][j]) = base + ((i − 1) × 8 + (j − 1)) × 4. The number of columns is upper bound − lower bound + 1. In this array A[4][7] is in the same place as A[3][6] in the zero-based array: 1120.

The layout has a practical side too: walking the matrix in memory order is faster. In C, if the outer loop walks the rows and the inner loop the columns, you read consecutive addresses and the cache is used well. Swap the loops and every step jumps a whole row ahead.

2Step by step

The drawing lays out a 3×4 matrix in row-major order in memory starting at address 1000; each element takes 4 bytes. Then the address of A[1][2] is computed.

row 0row 1row 2memory1[0][0]2[0][1]3[0][2]4[0][3]5[1][0]6[1][1]7[1][2]8[1][3]9[2][0]10[2][1]11[2][2]12[2][3]100011000100421004100831008101241012101651016102061020102471024102881028103291032103610103610401110401044121044index = 1 × 4 + 2 = 6address = 1000 + 6 × 4 = 1024
  1. The 3×4 matrix on top, 12 contiguous memory slots below. The numbers under the slots are addresses; each slot is 4 bytes.
  2. The four elements of row 0 go first: 1, 2, 3, 4. They take the addresses 1000 to 1012.
  3. Row 1 comes next: 5, 6, 7, 8. One row takes 4 elements, that is 16 bytes.
  4. Finally row 2: 9, 10, 11, 12. The whole matrix now sits in a single line.
  5. Let us find the address of A[1][2] = 7. In front of it are 1 full row (4 elements) and 2 elements in the same row: index 1 × 4 + 2 = 6.
  6. Multiply the index by the element size and add the base: 1000 + 6 × 4 = 1024. Slot 6 in memory does hold 7.

int rowMajorAddress(int base, int i, int j, int numCols, int size) {    int index = i * numCols + j;  // how many elements come before A[i][j]    return base + index * size;} int columnMajorAddress(int base, int i, int j, int numRows, int size) {    int index = j * numRows + i;  // j full columns first, then i elements    return base + index * size;}
int rowMajorAddress(int base, int i, int j, int numCols, int size) {    int index = i * numCols + j;  // how many elements come before A[i][j]    return base + index * size;} int columnMajorAddress(int base, int i, int j, int numRows, int size) {    int index = j * numRows + i;  // j full columns first, then i elements    return base + index * size;}
def row_major_address(base, i, j, num_cols, size):    index = i * num_cols + j  # how many elements come before A[i][j]    return base + index * size  def column_major_address(base, i, j, num_rows, size):    index = j * num_rows + i  # j full columns first, then i elements    return base + index * size

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1)

The formula does two multiplications and two additions; it does not depend on the size of the array. This is why index access in a two-dimensional array is .

Memory
O(1)

Only a few intermediate values are kept.

When you write A[i][j], the compiler does this calculation itself. For that it needs the number of columns; this is why you must write the column count when passing a matrix to a function in C.

4Common mistake

The most common mistake is multiplying by the number of rows instead of the number of columns in the row-major formula:

base + (i * numRows + j) * size   // wrong: row-major order uses the number of columns

In the A[5][8] example this wrong formula gives 1000 + (3 × 5 + 6) × 4 = 1084; the right answer is 1120. Multiply by the number of elements one row takes: in row-major order that is the number of columns. The second common mistake is forgetting to multiply the index by the element size; the result is then a count of elements, not bytes.

5Try it

int A[4][6] is stored in row-major order starting at address 2000, and an int takes 4 bytes. What is the address of A[2][5]?

Show the answer

The answer is A. In front of it are 2 full rows (2 × 6 = 12 elements) and 5 elements in the same row: 17. 2000 + 17 × 4 = 2068. 2088 is the column-major result.

An array with 3 rows and 4 columns is stored in column-major order. At which position in memory is A[2][1] (counting from 0)?

Show the answer

The answer is A. In front of it are 1 full column (3 elements) and 2 elements in the same column: 1 × 3 + 2 = 5. 9 is its position in row-major order.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.