34

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

e.g.

real, dimension(:) :: arr

and

real               :: arr(*)
DilithiumMatrix
  • 17,795
  • 22
  • 77
  • 119

2 Answers2

57

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.

francescalus
  • 30,576
  • 16
  • 61
  • 96
High Performance Mark
  • 77,191
  • 7
  • 105
  • 161
  • 6
    it should be noted the modern assumed shape `(:)` form requires an explicit interface. If you are working on old code with external subroutines you must use the `*` or provide the interface. – agentp Dec 08 '14 at 21:58
  • @agentp: For the sake of coding explicitly, it's better to provide the interface rather than the `*` whenever possible. Better yet, stick the routine in a module if appropriate, as interfaces aren't needed when modules are used. – jvriesem Jun 18 '18 at 17:23
18

High Performance Mark's answer explains the difference between the two statements - in short: yes, there's a difference; only one declares an assumed-size array - and the implications.

However, as dimension(:) but is mentioned, seemingly against not dimension(*), I'll add one thing.

real, dimension(:) :: arr1
real, dimension(*) :: arr2

is equivalent to

real :: arr1(:)
real :: arr2(*)

or even using dimension statements. [I don't want to encourage that, so I won't write out the example.]

The important difference in the question is the use of * and :, not dimension.

Perhaps there was some conflation of assumed-size with dummy argument? It is as a dummy argument where this choice is most common.

Community
  • 1
  • 1
francescalus
  • 30,576
  • 16
  • 61
  • 96