Explicit-shape and assumed-size arrays
Before assumed shape there were two older ways to pass an array: name its size in the dummy declaration, or leave the last dimension open with a star. Both are still everywhere, both still work, and both need the caller to say how many elements there are. This lesson writes each of them, passes a single element where an array is expected, and reads the one inquiry the star forbids.
An explicit-shape dummy
integer, intent(in) :: a(n) is an array of exactly n elements, where n is another dummy argument or a module constant the procedure can see. The dummy starts at one, and size(a) is then a compile-time fact rather than a runtime inquiry.
The file explicit.f90 calls one procedure twice with different sizes:
program explicit_demo
implicit none
integer :: x(6), i
x = [(i, i = 1, 6)]
call show(x, 3)
call show(x(2), 2)
contains
subroutine show(a, n)
integer, intent(in) :: n
integer, intent(in) :: a(n)
print *, "n =", n, " a =", a
print *, "size =", size(a), " lbound =", lbound(a)
end subroutine show
end program explicit_demo
gfortran -Wall -Wextra -std=f2018 explicit.f90 -o explicit
./explicit
n = 3 a = 1 2 3
size = 3 lbound = 1
n = 2 a = 2 3
size = 2 lbound = 1
The second call passes x(2), one element of the array, and gets a two-element dummy that starts at the element passed: this is sequence association, and it is the reason the two sizes agree in the output. The dummy is the same storage, read from a different starting point.
Sequence association
When an actual argument is an element of an array and the dummy is an array, the compiler passes the address of that element and the callee treats everything after it as the dummy. Nothing checks that the array is long enough: the association is a promise by the caller, and the second call above promises two elements from x(2), which the array can honour.
The same rule lets a rank-two array be passed to a rank-one dummy and read in memory order, which is how much of the old numerical code passes matrices to linear-algebra routines. It is correct when the two units agree on the layout, and it is invisible when they do not.
The assumed-size dummy
a(*) says that the last dimension has no declared extent: the procedure is told the number of elements by another argument and loops to it. The star is only allowed in the last dimension, and it exists for the routines of old libraries that were written before assumed shape.
The file assumed_size.f90:
program size_demo
implicit none
integer :: x(5), i
x = [(i * i, i = 1, 5)]
call total(x, 5)
contains
subroutine total(a, n)
integer, intent(in) :: n
integer, intent(in) :: a(*)
integer :: i, s
s = 0
do i = 1, n
s = s + a(i)
end do
print *, "n =", n, " total =", s
end subroutine total
end program size_demo
gfortran -Wall -Wextra -std=f2018 assumed_size.f90 -o assumed_size
./assumed_size
n = 5 total = 55
The loop needs both n and a, and a wrong n is not detected: the procedure reads whatever lies at that address. That is the price of the form, and it is why the two arguments belong next to each other in the list.
What a star cannot do
An assumed-size array has no size, so the inquiry is not allowed. The compiler says so instead of returning a number nobody could know:
program badsize
implicit none
integer :: x(3)
x = 0
call s(x)
contains
subroutine s(a)
integer, intent(in) :: a(*)
print *, size(a)
end subroutine s
end program badsize
gfortran -Wall -Wextra -std=f2018 badsize.f90 -o badsize
badsize.f90:10:18:
10 | print *, size(a)
| 1
Error: The upper bound in the last dimension must appear in the reference to the assumed size array 'a' at (1)
The same applies to ubound(a) and to any section that needs the end of the last dimension: the procedure must be given the size as an argument. A dummy of the shape a(n) or a(:) has no such limit, which is the first reason to prefer them in new code.
The bounds are a promise
None of the three forms checks the caller. An explicit shape trusts the n it was given, an assumed size trusts the loop bound, and an assumed shape trusts the descriptor built by the caller. A procedure that reads past its data reads the memory that follows, and the result is undefined rather than an error.
What a program can do about it is test what it is told: an explicit-shape dummy is usually paired with a check such as if (n < 0) error stop "bad size", and a build with -fcheck=bounds catches an index written outside the declared bounds of the dummy. The promise itself stays with the caller.
Exercises
- Call show a third time with show(x, 6). What does the output show, and why is it the whole array?
- Change the dummy of total to a(n). Does the program still build, and does the output change?
- Write a procedure with a(2, n) as its dummy and pass a three-column matrix to it. What does it see?
- Print ubound(a, 1) inside a procedure with a(n). Which number comes out?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
a(n)as a dummy argument mean?Answer
An array of exactly n elements, with n another argument or a visible constant
The bound is part of the declaration, so the shape is explicit.
-
What is
size(a)inside a procedure witha(n)?Answer
n
The declared bound is the size.
-
What does
call show(x(2), 2)pass when the dummy isa(n)?Answer
Two elements starting at x(2)
Sequence association starts the dummy at the element passed.
-
What does this program print?
program p implicit none integer :: x(4), i x = [(i, i = 1, 4)] call s(x(3), 2) contains subroutine s(a, n) integer, intent(in) :: n integer, intent(in) :: a(n) print *, sum(a) end subroutine s end program pAnswer
7
The dummy sees 3 and 4, whose sum is 7.
-
Where may the star of an assumed-size array appear?
Answer
In the last dimension only
The other dimensions must be explicit, so only the last can be open.
-
What does
size(a)give for an assumed-size dummya(*)?Answer
A compile error: the inquiry is not permitted
The size is not part of the dummy, so it cannot be asked for.
-
Which form tells the procedure its extent without a separate argument?
Answer
a(:)
An assumed-shape dummy carries the extent in its descriptor.
-
What must the caller of an assumed-size procedure supply?
Answer
The number of elements, as another argument
The star carries no extent, so the count must come separately.
-
What happens when a program passes an array shorter than the explicit dummy claims?
Answer
The procedure reads memory outside the array; the result is undefined
Nothing checks the promise unless a bounds check is built in.
-
Why does old numerical code pass matrices to rank-one routines?
Answer
Sequence association lets a matrix be read in memory order
A matrix is a contiguous run of values, column by column.
The programs on this page were built with GNU Fortran (Alpine 15.2.0) 15.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev