Sat. Jan 21st, 2023

Row-major and column-major

When a multi-dimensional array (# dimensions > 1) is stored, there are two ways in which the data can be arranged in memory:

  • Each item in a row is stored consecutively, one row at a time
  • Each item in a column is stored consecutively, one column at a time

In the diagram below, follow the line to see the order in which the items are stored in RAM:

This could have major implications for the programmer: if you are using a language where direct referencing or pointers are common, you could inadvertently retrieve or amend the wrong item within the array.

Luckily, higher-level languages such as C#, Python, Java and so on abstract this information and there is no danger of getting or setting the wrong value.