Resizing of Multi-Dimensional Arrays when Passed

后端 未结 2 1615
礼貌的吻别
礼貌的吻别 2021-01-16 10:53

All references to arrays in this post are multi-dimensional.

I came to know that when an array is passed a subroutine, it can be declared with different dimensions/s

2条回答
  •  盖世英雄少女心
    2021-01-16 11:24

    Multi-dimensional arrays in Fortran are stored in column-major order. In memory, they the elements are linear order and the memory offset is calculated from the multi-dimensional indices. The equation is given at http://en.wikipedia.org/wiki/Row-major_order. The Fortran compiler will use that equation and calculate an position in memory based on the dimensions that you provide. To figure out the correspondence between elements when you declare a multi-dimensional array with different dimensions, apply the equation twice using the different dimensions. The compiler doesn't move values in memory. It calculates position in memory from the index values, based on the dimensions.

    There are cases where the code generated by the Fortran compiler will copy values, creating a temporary array. e.g., if the actual array argument in the call involves a stride, the compiler might need to create a contiguous temporary array to be compatible with the argument of the subroutine.

提交回复
热议问题