1

Is it possible to make the dimension of the array allocatable? (not just the size of a dimension)

i.e., something giving:

REAL, DIMENSION(:,:,: ... n), ALLOCATABLE :: array

I mean this in an array of arrays sense, but can we do it preserving Fortran's easily accessible array structure? There was this, but the first answer does not satisfy this need. The second answer uses pointers. Will that work?

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
physkets
  • 183
  • 3
  • 12
  • 2
    A similar question is [this one](https://stackoverflow.com/q/31974786). – francescalus Jul 12 '16 at 12:39
  • Or this one: http://stackoverflow.com/q/7500920/2737715 – Alexander Vogt Jul 12 '16 at 12:52
  • 2
    This is something of an FAQ. But what is less frequent is a compelling reason for needing this facility. What's yours ? – High Performance Mark Jul 12 '16 at 13:29
  • Thanks for those two links. I suppose I did not search well enough before posting. – physkets Jul 13 '16 at 05:40
  • @HighPerformanceMark as is described in one of the links above, it is to be able to define the 'dimension' of my simulation in the program, and use Fortran's simple array operations to make the code simpler. – physkets Jul 13 '16 at 05:42
  • 1
    I found that [intel](https://software.intel.com/en-us/node/579892) has implemented 'assumed-rank' arrays and that [gfortran](https://gcc.gnu.org/onlinedocs/gfortran/Further-Interoperability-of-Fortran-with-C.html) is going to too. The following works: `REAL, DIMENSION(..) :: foo(n), goo(n,n)` But this doesn't: `REAL, DIMENSION(..), ALLOCATABLE :: foo, goo` with `error #8775: An assumed rank object must be a DUMMY argument.` – physkets Jul 13 '16 at 05:49
  • @physkets In your first case, `DIMENSION(..)` is simply ignored since you have explicitly specified the shape. An assumed rank object must be a DUMMY argument, which means that it is not a new type of array, it just refers to an actual array you passed in. – Youjun Hu Jun 02 '23 at 06:39

2 Answers2

4

No, it is not possible to have an array with variable rank. From the Fortran 2008 Standard, Cl. 2.4.6 "Array":

1 An array may have up to fifteen dimensions, and any extent in any dimension. The size of an array is the total number of elements, which is equal to the product of the extents. An array may have zero size. The shape of an array is determined by its rank and its extent in each dimension, and is represented as a rank-one array whose elements are the extents. All named arrays shall be declared, and the rank of a named array is specified in its declaration. The rank of a named array, once declared, is constant; the extents may be constant or may vary during execution.

[Emphasis mine.]

However, you could have a one-dimensional array with extent product(extent in each dimension), and index the elements appropriately.

You could even have multi-dimensional pointers associated with these 1D-arrays. This would take care of the indexing for you, but (as given in the citation), is limited to 15 dimensions for Standard Fortran.

Alexander Vogt
  • 17,879
  • 13
  • 52
  • 68
0

Is it possible to make the dimension of the array allocatable? Yes,Fortran 2018 standard specifies the syntax for this: e.g., real :: a(..)

However, an assumed rank object must be a DUMMY argument, which means that it is not a new type of array that you can create in your main program, it just refers to an actual array you passed in. For example:

REAL :: a0
REAL :: a1(10)
REAL :: a2(10, 20)
REAL,allocatable :: a3(:,:,:)

CALL sub1(a0)
CALL sub1(a1)
CALL sub1(a2)
CALL sub1(a3)

CONTAINS
    SUBROUTINE sub1(a)
        REAL :: a(..)
        PRINT *, RANK(a)
    END
END

It will output:

           0
           1
           2
           3
Youjun Hu
  • 991
  • 6
  • 18
  • 1
    Still, the rank of the particular array is fixed. Just arrays of different (fixed) ranks can be passed to the subroutine. Also, the possibilities of what can be done with such arguments are very limited. – Vladimir F Героям слава Jun 04 '23 at 11:38