Indexing, bounds and zero-sized arrays
An index in Fortran is a position in the value, and the first position is 1 unless the declaration says otherwise. The bounds belong to the array and are available to the program, which is why a loop can be written without knowing the size. This lesson reads the bounds, changes them, walks out of them with the check on, and finishes with the array that has no elements.
Indexing and the lower bound
integer :: a(3) has the bounds 1 and 3. The declaration may state both with a colon, and the form integer :: b(0:4) has five elements numbered 0 to 4. The lower bound is part of the type: a program that passes b to a procedure and receives it as a(5) sees different indices for the same storage, which is why a procedure that needs a particular numbering declares it.
The file bounds.f90:
program bounds
implicit none
integer :: a(3), b(0:4)
integer :: i
a = [10, 20, 30]
b = [(i, i = 0, 4)]
print *, "a :", a
print *, "lbound(a):", lbound(a), " ubound(a):", ubound(a), " size:", size(a)
print *, "b :", b
print *, "lbound(b):", lbound(b), " ubound(b):", ubound(b), " size:", size(b)
print *, "shape(a) :", shape(a)
print *, "a(1), a(3):", a(1), a(3)
print *, "b(0), b(4):", b(0), b(4)
end program bounds
gfortran -Wall -Wextra -std=f2018 bounds.f90 -o bounds
./bounds
a : 10 20 30
lbound(a): 1 ubound(a): 3 size: 3
b : 0 1 2 3 4
lbound(b): 0 ubound(b): 4 size: 5
shape(a) : 3
a(1), a(3): 10 30
b(0), b(4): 0 4
The constructor [(i, i = 0, 4)] fills b with its own indices, so the output shows that position 0 holds 0. A loop over b counts from lbound(b) to ubound(b), and a loop that counted from 1 to size(b) would read one element past the end.
LBOUND, UBOUND and SIZE
The three intrinsics answer three different questions, and a rank-1 array hides the difference:
- lbound(a) and ubound(a) are the first and last valid index.
- size(a) is the number of elements, which is ubound - lbound + 1.
- shape(a) is an array of the extents, one per dimension.
For a rank-2 array each of them takes a second argument that names the dimension: size(m, 1) is the number of rows. The last element of a dimension is always ubound(m, d), which is the expression a loop should use.
The bounds check
An index outside the bounds is undefined behaviour, and the compiler can turn it into a message with -fcheck=bounds. The program below reads one element past the end on purpose.
The file oob.f90:
program oob
implicit none
integer :: a(3)
a = [1, 2, 3]
print *, "a(1):", a(1)
print *, "a(4):", a(4)
end program oob
gfortran -Wall -Wextra -std=f2018 -fcheck=bounds oob.f90 -o oob
oob.f90:7:22:
7 | print *, "a(4):", a(4)
| 1
Warning: Array reference at (1) is out of bounds (4 > 3) in dimension 1
The warning is possible because both the bound and the index are constants. The build continues, and the run stops at the offending line:
GFORTRAN_ERROR_BACKTRACE=0 ./oob
a(1): 1
At line 7 of file oob.f90
Fortran runtime error: Index '4' of dimension 1 of array 'a' above upper bound of 3
The run stops with exit status 2, and the second line names the array, the dimension, the index and the bound. Without the flag the program would read the four bytes after the array, print a number that depends on what the compiler put there, and continue: a wrong answer instead of a message, which is the whole argument for building test runs with the check on.
Zero-sized arrays
An array may have no elements: integer :: a(0) is a legal declaration and size(a) is 0. It is the natural result of a filter that found nothing, and a program that handles it without a special case is a program that survives an empty input file.
The file empty.f90:
program empty
implicit none
integer :: a(0)
print *, "size :", size(a)
print *, "sum :", sum(a)
print *, "count :", count(a > 0)
print *, "any :", any(a > 0)
print *, "all :", all(a > 0)
print *, "maxval:", maxval(a)
end program empty
gfortran -Wall -Wextra -std=f2018 empty.f90 -o empty
./empty
size : 0
sum : 0
count : 0
any : F
all : T
maxval: -2147483648
The identity values
Every reduction has an answer for the empty case, and the answer is the identity of its operation:
- sum is 0, because adding nothing changes nothing.
- product is 1, for the same reason.
- count is 0 and any is false, because no element satisfies the condition.
- all is true, because no element violates it. That is the line that surprises a reader and is correct: the statement "every element is greater than zero" holds when there are no elements.
- maxval is -huge of the type, here -2147483648, which is the smallest integer and the identity for a maximum.
A program that prints a maximum therefore prints -2147483648 for an empty input, and a program that must not has to test size(a) > 0 first. The value is not a bug in the compiler: it is the defined result, and knowing it is cheaper than finding it in a results table.
Exercises
- Print ubound(b) - lbound(b) + 1 for the first program. Does it equal size(b)?
- Change a(4) to a(3) in the out-of-bounds program and rebuild with the check. What does the warning say now?
- Declare integer :: c(-2:2) and fill it with its own indices. Which indices are valid?
- Sum the empty array with product instead of sum. What does it print?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is the lower bound of
integer :: a(3)?Answer
1
The default lower bound is one; the declaration may state another.
-
How many elements does
integer :: b(0:4)have?Answer
5
The count is 4 - 0 + 1, which is five.
-
What does
-fcheck=boundsdo?Answer
It tests every subscript at run time and reports the failing line
The check is a runtime test that turns a silent read into a message.
-
What is
size(a)for a zero-sized array?Answer
0
An array with no elements has size zero and is legal.
-
What does
sumreturn for a zero-sized array?Answer
0
Zero is the identity of addition, so the sum of nothing is zero.
-
What does
allreturn for a zero-sized array?Answer
.true.
No element violates the condition, so the statement holds vacuously.
-
What is
maxvalof a zero-sized integer array here?Answer
-2147483648, which is -huge
The identity for a maximum is the smallest value of the type.
-
What does this program print?
program p implicit none integer :: a(3) a = [5, 6, 7] print *, a(2) + a(3) end program pAnswer
13
a(2) is 6 and a(3) is 7, and their sum is 13.
-
How should a loop over an array with a custom lower bound count?
Answer
From lbound to ubound
The valid indices are the bounds, whatever they are.
-
What happens to an out-of-bounds read without the bounds check?
Answer
The program reads whatever is next in memory and may print a wrong value
The behaviour is undefined, and a wrong value is the common case.
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