Pointers and TARGET
A Fortran pointer is an alias: a name that refers to storage owned by a variable that has been declared a target. It is not an address arithmetic type and it is not an allocator by itself; what it does is give a second name to something that already exists, so that a procedure can see the caller's array without copying it and a data structure can refer to a value that moves. This lesson makes a pointer point at a scalar, at an array section and at a whole array, and shows why the arrow and the equals sign are different operations.
POINTER and TARGET
Two declarations are needed. The variable that owns the storage is declared real, target :: x, and the pointer that may refer to it is real, pointer :: p. The target attribute tells the compiler that the address of this variable may be taken, which is what lets it keep the value in a register otherwise. A pointer without a target is a pointer to nothing, and associated(p) answers .false.
The file ptr.f90:
program ptrdemo
implicit none
real, target :: x, y
real, pointer :: p, q
real, target :: a(5)
real, pointer :: s(:)
x = 1.5
y = 2.5
p => x
print '(a,f6.1)', 'p -> x = ', p
p = 9.0
print '(a,f6.1)', 'x after p=9 = ', x
q => y
print '(a,l1)', 'q is associated = ', associated(q)
print '(a,l1)', 'q and p share data = ', associated(q, p)
q = p
print '(a,l1)', 'q and p share data = ', associated(q, p)
print '(a,f6.1)', 'y after q=p = ', y
a = [1.0, 2.0, 3.0, 4.0, 5.0]
s => a(2:4)
print '(a,3f5.1)', 's -> a(2:4) =', s
s(1) = 20.0
print '(a,5f5.1)', 'a =', a
s => a
print '(a,i0)', 'size(s) = ', size(s)
end program ptrdemo
gfortran -Wall -Wextra -std=f2018 ptr.f90 -o ptr
./ptr
p -> x = 1.5
x after p=9 = 9.0
q is associated = T
q and p share data = F
q and p share data = F
y after q=p = 9.0
s -> a(2:4) = 2.0 3.0 4.0
a = 1.0 20.0 3.0 4.0 5.0
size(s) = 5
Four lines carry the lesson. p => x made a second name for x, and p = 9.0 wrote 9.0 into x: a pointer read and written like the variable it refers to. q = p then copied the value and left the two pointers on different storage, which the two F answers show, and y became 9.0. Assigning a pointer with = is the same as assigning the variable it points at, never an association.
The arrow is not the equals sign
p => x is a pointer assignment: it sets where p looks and touches no data, so x keeps its value. p = x is an ordinary assignment to the target: it copies the value of x into whatever p points at, and if p is not associated it is a run-time error. The same pair exists for arrays, and confusing them is the first bug every reader of old Fortran meets.
associated(q, p) asks whether two pointers refer to the same storage, and associated(q) asks whether q refers to anything at all. Both are needed: associated(q) being true says nothing about what q shares with p.
A pointer to a section
s => a(2:4) makes s a pointer to the middle three elements of a, and the array the pointer sees is an array of its own with bounds one to three. It is a view, not a copy: s(1) = 20.0 wrote into a(2), which is why the array printed as one, twenty, three, four, five. That is how a procedure takes a column of a matrix without copying it, and it is also how a bug can hide: the pointer has bounds that the array does not.
A pointer to a section is only valid while the target lives. The array is a local of this program, so the section is safe here; a returned pointer to a local array is a dangling reference, and the language gives no error for it.
A pointer to a whole array
s => a re-associates the same pointer with the whole array, and size(s) becomes five. A pointer may be re-associated as often as the program likes, and a pointer declared real, pointer :: s(:) takes targets of any length. That is the classic way to pass a large array to a procedure with an assumed-shape dummy without copying, and Fortran's answer to it is usually the plain dummy, which does the same and is safer.
A target that is not a TARGET
Pointing at a variable that was not declared a target is refused at compile time:
program badtarget
implicit none
real :: x
real, pointer :: p
x = 1.5
p => x
print *, p
end program badtarget
gfortran -Wall -Wextra -std=f2018 badtarget.f90 -o badtarget
badtarget.f90:7:7:
7 | p => x
| 1
Error: Pointer assignment target is neither TARGET nor POINTER at (1)
The rule exists because the target attribute is a promise the compiler relies on: without it, the value of x may sit in a register and a pointer to it would be a lie. The fix is to add target to the declaration, and the next lesson takes the association rules further with nullify and the cases where a pointer may be left pointing nowhere.
What a pointer costs
A pointer is a descriptor, not a bare address: it carries the address, the bounds, the strides and the type of its target, which is why a pointer to a section can be an array of three elements. Reading through it costs one indirection, and no pointer can be kept in a register, so a loop that goes through a pointer is slower than one that goes through a dummy argument the compiler can analyse.
The other cost is the one this course keeps returning to: a pointer does not own its target, so a dangling pointer is possible and the compiler cannot check it. That is why an allocatable is the default in this course and a pointer needs one of the three reasons of section 087.
Exercises
- Add p => y after the first block and print associated(p, q). What is the answer, and what changed in x and y?
- Write q => p instead of q = p and rerun. Which two lines of the output change?
- Make s point at a(1:5:2) and print it. How many elements does the pointer hold, and what are their values?
- Remove target from a and rebuild. Which line is refused?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
real, target :: xadd to a declaration?Answer
Permission for a pointer to refer to x
Only a target may be the target of a pointer assignment.
-
What does
p => xdo?Answer
Makes p refer to the storage of x
The arrow is a pointer assignment.
-
What does
p = xdo when p is associated with y?Answer
Copies the value of x into y
An ordinary assignment writes the target.
-
What does this program print?
program p implicit none real, target :: x real, pointer :: q x = 2.0 q => x q = 5.0 print *, x, q end program pAnswer
5.00000000 5.00000000
q refers to x, so writing through q writes x.
-
What does
s => a(2:4)give?Answer
A view of three elements that writes into a
A section pointer aliases the target.
-
What does
associated(q, p)ask?Answer
Whether q and p refer to the same storage
The two-argument form compares associations.
-
What does gfortran report when a pointer is pointed at a plain variable?
Answer
Error: pointer assignment target is neither TARGET nor POINTER
The target attribute is required.
-
Why does a pointer loop run slower than a dummy-argument loop?
Answer
The pointer adds an indirection and cannot be kept in a register
The target may alias anything, so every read goes through it.
-
What does a pointer descriptor carry besides an address?
Answer
Bounds, strides and the type of the target
That is why a section pointer can be an array.
-
Why is an allocatable the default in this course rather than a pointer?
Answer
It owns its storage and cannot dangle
A pointer needs a reason and a lifetime the program proves.
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