How Matrices Are Stored In Memory

Did you ever hear someone say that computers are stupid? Well, in a way it's true. In general Computers have no idea what exactly a matrix is. For them it's just a stream of numbers. So when you say,

double matrix[10][10];

or

Real*8 matrix(10,10)

the information stored in these matrices has to be converted into a stream of numbers and unfortunately the way this is done is different for C and Fortran.


In C and in Pascal matrices are stored in row major order.
You start with the first element in the first row, then comes the second element in the first row and so on until you reached the end of the first row. Then you take the first element of the second row and so on.
The matrix,

would be stored as,

a b c d e f g h i


In Fortran matrices are stored in column major order
You start with the first element of the first row, then comes the first element of the second row and so on until you reached the end of the first column. Then you take the first element of the second column and so on.
The matrix,

would be stored as,

a d g b e h c f i


Back to information about LAPACK in C programs.