Is there any difference between these two methods of declaring an assumed-size array?
e.g.
real, dimension(:) :: arr
and
real :: arr(*)
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With