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

Is there a difference between these two methods of declaring an array of an estimated size?

eg.

real, dimension(:) :: arr

      

and

real               :: arr(*)

      

+19


source to share


2 answers


The form

real, dimension(:) :: arr

      

declares an array of the intended shape, and the shape

real :: arr(*)

      



declares an array of estimated size.

And, yes, there are differences between their use. The differences arise due to the fact that, approximately, the compiler "knows" the shape of the array of the intended shape, but not the array of the assumed size. The additional information available to the compiler means that, among other things, arrays of intended shapes can be used in expressions of the entire array. An array of estimated size can only be used in whole array expressions when it is an actual argument in a procedure reference that does not require an array shape. Oh, and also in calling the internal lbound

, but not in calling the internal ubound

. There are other subtle and not so subtle differences that your close reading of the standard or good Fortran book will reveal.

Some advice for new Fortran programmers is to use the intended shape arrays whenever possible. They weren't available until Fortran 90, so you'll see a lot of suggested size arrays in the old code. Arrays with intended shapes are better in new code because functions shape

and size

can be used to query their sizes to avoid errors out of bounds and up to allocate

arrays whose sizes depend on the sizes of the input arrays.

+33


source


The response to the high performance token explains the difference between the two statements - in one word: yes, there is a difference; only one declares an array of estimated size - and the consequences.

However, as dimension(:)

, but mentioned, it would seem, not against dimension(*)

, I will add one.

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

      

equivalent to



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

      

or even using operators dimension

. [I don't want to encourage this, so I won't write out an example.]

An important difference in the question is the use of *

and :

, not dimension

.

Perhaps there was some kind of merging of the estimated size with the bogus argument? It's like a bogus argument where this choice is most common.

+11


source







All Articles