# Row Major Vs Column Major Order: 2D arrays access 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.

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. Mar 24 '17 at 9:17
• It's because an array A[10][10] can be cast to an array A[][10]. Mar 24 '17 at 9:28

Both Row major and column major order takes same amount of time. Since both have same number of operations. i.e. Lets take A[m][n]. Then the corresponding row major and column major formulas are :

1. row major = [base address + (i*n + j) * size of the data_type].
2. Column major = [base address + (j*m + i) * size of the data_type].

From the above formulas we can define that both have 4 operations to perform. So no factor affects the time of accessing the data.

• Your answer is correct if memory access takes O(1) irrespective of the underlying structure used for data storage but that's not the case always when you are working at low level. Do remember storing data at continuous memory will always provide must faster lookup than when the data is stored at different memory locations. Mar 19 at 10:34