# Row Major vs Column Major order access 2D arrays in programming languages

Programmers prefer accessing a 2D array in Row-Major Order rather than Column-Major Order, Why?

Are there some advantages/benefits of accessing a 2D array in row-major as compare to column-major?

Like in C programming, programmers prefer row-major rather than column-major due to the way memory is being allocated to a 2D array when defined statically or dynamically.

## 2 Answers

The way you access the array affects performance.

It depends on how the matrix is represented and stored in memory. Often a matrix is stored in row-major order, so that consecutive elements of a row are contiguous in memory. Reading memory in contiguous locations is faster than jumping around among locations. As a result, if the matrix is stored in row-major order, then iterating through its elements sequentially in row-major order may be faster than iterating through its elements in column-major order.

Of course, if the matrix is stored in column-major order, the reverse will be true.

See https://en.wikipedia.org/wiki/Row-_and_column-major_order for more on the subject.

It's a completely arbitrary decision. In Fortran, for example, "people" prefer column-major order, whereas in C "they" prefer row-major order.

In C the row-major order is forced due to reasons of syntax and the fact that dimensions are not always known in advanced. Fortran doesn't have that problem, so according to your line of argument, column-major order is the more "natural", the other order being used in C for technical reasons.

• Sir what you mean by "Due to reasons of syntax and the fact that dimensions are not always known in advanced", can you give me an example to understand this statement. It will be very helpful. – strikersps Mar 24 '17 at 9:17
• It's because an array A[10][10] can be cast to an array A[][10]. – Yuval Filmus Mar 24 '17 at 9:28