2-6  ARRAY STORAGE ORDER 
 ************************

 Array elements are stored in memory one after the other, if you 
 have a one-dimensional array, everything is simple, higher array 
 indices correspond to higher memory addresses.

 With multi-dimensional arrays it is more complicated, there are 
 two practical possibilities: the COLUMN MAJOR ORDER used in FORTRAN, 
 and ROW MAJOR ORDER used in C.

 Let's examine a small matrix:

        A11  A12  A13
        A21  A22  A23
        A31  A32  A33

 We can store the elements in memory like this:

   A11 A21 A31 A12 A22 A32 A13 A23 A33     ---> Higher addresses

 This is FORTRAN's Column major order, the first array index 
 varies most rapidly.


 The alternative method is:

   A11 A12 A13 A21 A22 A23 A31 A32 A33     ---> Higher addresses

 That is C row major order, the last array index varies most rapidly. 

 By the way, if the matrix is symmetric the values stored in memory
 will be the same in both methods. 


 Whole array operations
 ----------------------
 When you WRITE an array in FORTRAN using just the array name, 
 you will get the elements written in FORTRAN's order - the 
 transpose of the mathematical order, the small 3x3 array:

      A11  A12  A13
      A21  A22  A23
      A31  A32  A33

 Will be written (with a format that allows 3 numbers per line): 
 
      A11  A21  A31    
      A12  A22  A32
      A13  A23  A33


 Try this small program:

      PROGRAM ARRELM
C     ------------------------------------------------------------------
      INTEGER
     *          I, J,
     *          ARRAY(9,9)
C     ------------------------------------------------------------------
      DO I=1,9
        DO J=1,9
          ARRAY(I,J) = 10*I + J
        ENDDO
      ENDDO
C     ------------------------------------------------------------------
      WRITE(UNIT=*,'(1X,9I4)') ARRAY
C     ------------------------------------------------------------------
      END

 If you always reference array elements using array indexes, and pass
 whole arrays to subprograms, the memory order doesn't matter, except
 when optimizing program memory use.

 More 'sophisticated' (and worse) programs may depend on tricks based
 on the internal memory order.


Return to contents page