1.5.7 Memory Sequence of Multi-Dimensional Arrays

Multi-dimensional arrays (of up to 7-D) can be defined in FORTRAN77. However, the actual memory system does not have a multi-dimensional structure, and it is managed one-dimensionally. The order for unfolding a multi-dimensional array into a 1-D array is defined in the FORTRAN77 standard, and so it is possible to combine multi-dimensional arrays to 1-D arrays (to give a 1-D name to multi-dimensional arrays.) For example, in the case of 

      REAL         X(6), Y(2,3)

      COMPLEX      Z(3)

      EQUIVALENCE (X,Y,Z)



the variables X, Y, Z all are arrays with 6 words (48 bytes), and they occupy the same memory area according to the EQUIVALENCE statement. The variables are aligned in the following order.



X(1) X(2) X(3) X(4) X(5) X(6)
Y(1,1) Y(2,1) Y(1,2) Y(2,2) Y(1,3) Y(2,3)
Re(Z(1)) Im(Z(1)) Re(Z(2)) Im(Z(2)) Re(Z(3)) Im(Z(3))


The 2-D array Y is unfolded into a 1-D array in a way so that the left subscript changes first. Complex number data Z is unfolded as a sequence of 2 floating-point numbers. Therefore, the value of  X(3) will be exactly the same as Y(1,2), Re(Z(2)).

This rule is actively used in many programs to treat multi-dimensional arrays as 1-D arrays or to treat complex-number data as floating-point-number data. This rule is close to the hardware of computers, so it may appear machine-dependent, but this is "standard language" defined in FORTRAN77 standard.