Multi-dimensional array ordering

From Helpful
Jump to: navigation, search
This article/section is a stub — probably a pile of half-sorted notes, is not well-checked so may have incorrect bits. (Feel free to ignore, fix, or tell me)


When you have to serialize matrix data onto linear storage, you can choose between:

  • Row-major means that elements logically adjacent within a row are adjacent in memory.
  • Column-major that adjacent column elements are adjacent in memory.



Most of the time you don't really have to think about this. Since basic arrays types are defined by the language you are using, the language has already made the choice for you.

When writing your own code to save and load, you'll also implicitly be consistent. When using built-in functions, it's typically the same.


There are some libraries that can have an opinion different from the language you use them from, e.g. based on the type of operation likely to be common. The idea is that iterating over contiguous memory will be faster due to spatial locality - the next bit is likely to already be in fast cache.

And iterating in the language-native way will do this no matter how you view this data.



You should only need to think about this when

saved data is effectively memory dumps, and can come from a source with different ordering from your code's / your current platform's
you work with both orderings from the same code, e.g. when mixing C and Fortran