Fortran Arrays Array nature specification: rank and shape


Example

The dimension attribute on an object specifies that that object is an array. There are, in Fortran 2008, five array natures:1

  • explicit shape
  • assumed shape
  • assumed size
  • deferred shape
  • implied shape

Take the three rank-1 arrays2

integer a, b, c
dimension(5) a    ! Explicit shape (default lower bound 1), extent 5
dimension(:) b    ! Assumed or deferred shape
dimension(*) c    ! Assumed size or implied shape array

With these it can be seen that further context is required to determine fully the nature of an array.

Explicit shape

An explicit shape array is always the shape of its declaration. Unless the array is declared as local to a subprogram or block construct, the bounds defining shape must be constant expressions. In other cases, an explicit shape array may be an automatic object, using extents which may vary on each invocation of a subprogram or block.

subroutine sub(n)
  integer, intent(in) :: n
  integer a(5)   ! A local explicit shape array with constant bound
  integer b(n)   ! A local explicit shape array, automatic object
end subroutine

Assumed shape

An assumed shape array is a dummy argument without the allocatable or pointer attribute. Such an array takes its shape from the actual argument with which it is associated.

integer a(5), b(10)
call sub(a)   ! In this call the dummy argument is like x(5)
call sub(b)   ! In this call the dummy argument is like x(10)

contains

  subroutine sub(x)
    integer x(:)    ! Assumed shape dummy argument
  end subroutine sub

end

When a dummy argument has assumed shape the scope referencing the procedure must have an explicit interface available for that procedure.

Assumed size

An assumed size array is a dummy argument which has its size assumed from its actual argument.

subroutine sub(x)
  integer x(*)   ! Assumed size array
end subroutine

Assumed size arrays behave very differently from assumed shape arrays and these differences are documented elsewhere.

Deferred shape

A deferred shape array is an array which has the allocatable or pointer attribute. The shape of such an array is determined by its allocation or pointer assignment.

integer, allocatable :: a(:)
integer, pointer :: b(:)

Implied shape

An implied shape array is a named constant which takes its shape from the expression used to establish its value

integer, parameter :: a(*) = [1,2,3,4]

The implications of these array declarations on dummy arguments are to be documented elsewhere.


1A Technical Specification extending Fortran 2008 adds a sixth array nature: assumed rank. This is not covered here.

2 These can equivalently be written as

integer, dimension(5) :: a
integer, dimension(:) :: b
integer, dimension(*) :: c

or

integer a(5)
integer b(:)
integer c(*)