3
$\begingroup$

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.

$\endgroup$
4
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
2
$\begingroup$

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.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ 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. $\endgroup$ – strikersps Mar 24 '17 at 9:17
  • 3
    $\begingroup$ It's because an array A[10][10] can be cast to an array A[][10]. $\endgroup$ – Yuval Filmus Mar 24 '17 at 9:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.