Assumed size arrays: Colon vs. asterisk - DIMENSION(:) arr vs. arr(*)

DilithiumMatrix picture DilithiumMatrix · Jul 3, 2014 · Viewed 12.5k times · Source

Is there any difference between these two methods of declaring an assumed-size array?

e.g.

real, dimension(:) :: arr

and

real               :: arr(*)

Answer

High Performance Mark picture High Performance Mark · Jul 3, 2014

The form

real, dimension(:) :: arr

declares an assumed-shape array, while the form

real :: arr(*)

declares an assumed-size array.

And, yes, there are differences between their use. The differences arise because, approximately, the compiler 'knows' the shape of the assumed-shape array but not of the assumed-size array. The extra information available to the compiler means that, among other things, assumed-shape arrays can be used in whole-array expressions. An assumed-size array can only be used in whole array expressions when it's an actual argument in a procedure reference that does not require the array's shape. Oh, and also in a call to the intrinsic lbound -- but not in a call to the intrinsic ubound. There are other subtle, and not-so-subtle, differences which your close reading of the standard or of a good Fortran book will reveal.

Some advice for new Fortran programmers is to use assumed-shape arrays when possible. They were not available before Fortran 90, so you will see lots of assumed-size arrays in old code. Assumed-shape arrays are better in new code, because the shape and size functions can be used to query their sizes to avoid out-of-bounds error and to allocate arrays whose dimensions depend on the dimensions of input arrays.