Association, ASSOCIATED and NULLIFY
A pointer has a state and a target, and the state is what separates a pointer from an address. A pointer is associated or it is null, associated asks which, nullify makes it null again, and a pointer dummy argument lets a procedure move the caller's pointer. This lesson follows one pointer through all of those states, then looks at the one state the language leaves undefined: a pointer whose target has been deallocated.
NULLIFY and the null status
real, pointer :: p => null() declares a pointer whose status is not associated before the first statement, and nullify(p) puts it back into that state later. A null pointer is the safe value: associated(p) answers .false., and a program that tests before it reads never touches a target that is not there.
The file assoc.f90:
program assocdemo
implicit none
real, target :: x, y
real, pointer :: p => null(), q
real, allocatable, target :: buf(:)
real, pointer :: view(:)
x = 1.0
y = 2.0
p => x
q => x
print '(a,l1)', 'p and q on x = ', associated(p, q)
nullify(p)
print '(a,l1)', 'p after nullify = ', associated(p)
print '(a,l1)', 'q after nullify(p) = ', associated(q)
allocate(buf(4))
buf = [1.0, 2.0, 3.0, 4.0]
view => buf(2:3)
print '(a,2f5.1)', 'view =', view
view(1) = 20.0
print '(a,4f5.1)', 'buf =', buf
call repoint(p, y)
print '(a,f5.1)', 'p now reads y = ', p
deallocate(buf)
print '(a,l1)', 'view after deallocate = ', associated(view)
contains
subroutine repoint(ptr, t)
real, pointer, intent(inout) :: ptr
real, target, intent(in) :: t
ptr => t
end subroutine repoint
end program assocdemo
gfortran -Wall -Wextra -std=f2018 assoc.f90 -o assoc
./assoc
p and q on x = T
p after nullify = F
q after nullify(p) = T
view = 2.0 3.0
buf = 1.0 20.0 3.0 4.0
p now reads y = 2.0
view after deallocate = T
Four different things happened to p, and the two associated forms are both in the run. associated(p, q) is true while both point at x, and after nullify(p) that same question is still about the pair while associated(p) alone answers .false.. Nullifying a pointer never touches another pointer to the same target: q stayed associated.
A pointer dummy argument
real, pointer, intent(inout) :: ptr is a dummy that receives the pointer itself, not the value it points at. ptr => t inside repoint therefore changes the caller's pointer, which is why the last print reads 2.0 from y through p.
A plain dummy, without the pointer attribute, works with addresses and copies too: an assumed-shape array argument is passed by reference without a copy, and an intent(inout) scalar is the caller's variable. The pointer dummy exists for the one thing the others cannot do, which is to change which target the caller's name refers to.
A view of an array
view => buf(2:3) makes a pointer to two elements of the allocatable array, and the write through it landed in buf(2). The pointer is a descriptor with its own bounds, so size(view) is two, and the array sees the change because the storage is the same.
This is the cheap way to give a procedure a sub-array when its interface cannot take a section, and the reason section 034 says a section is not a copy. It is also the way an off-by-one becomes invisible: a view of buf(2:3) has elements one and two, and code that also indexes buf with them is wrong in a way no compiler catches.
After the target dies
The last line of the run is the one to be careful with. buf was deallocated, so the storage that view referred to is gone, and the standard says the association status of view becomes undefined: not associated, not null, but unspecified. This build answers T to associated(view), and reading view(1) afterwards would read freed memory.
An undefined status is not a bug the compiler can report, and the same program may answer differently on another compiler or another optimisation level. The rule is therefore a discipline, not a check: nullify a pointer before you deallocate its target, or make the pointer an allocatable, which cannot dangle because it owns what it refers to.
The same applies to an uninitialised pointer: without => null() its status is undefined from the declaration, and this build reports it as associated. A pointer that is tested before it has ever been assigned is a pointer whose answer means nothing.
A non-pointer actual argument
A pointer dummy needs a pointer, and a plain variable is not one:
program badptrarg
implicit none
real, target :: x
real, pointer :: p => null()
x = 1.0
p => x
call repoint(x)
print *, p
contains
subroutine repoint(ptr)
real, pointer, intent(inout) :: ptr
ptr => null()
end subroutine repoint
end program badptrarg
gfortran -Wall -Wextra -std=f2018 badptrarg.f90 -o badptrarg
badptrarg.f90:8:15:
8 | call repoint(x)
| 1
Error: Actual argument for 'ptr' at (1) must be a pointer or a valid target for the dummy pointer in a
pointer assignment statement
The wording is precise about the second half: a variable with the target attribute may be passed to a pointer dummy too, because the call can make a pointer of it. What it cannot do is bind a pointer to a variable that has no address to give, and a dummy without target is such a variable.
The rules worth keeping
Three rules cover this lesson. Initialise every pointer with => null() and test with associated before the first use. Nullify a pointer before deallocating its target, and prefer an allocatable when the pointer would own the storage. And take associated with two arguments seriously: it is the only way to ask whether two names refer to the same object, and it is the question a program that shares data has to answer before it writes.
With those rules a pointer behaves like a reference, which is what most code wants. Without them it behaves like a C pointer with a nicer syntax, including the part where the bug shows up three functions later.
Exercises
- Move nullify(view) before deallocate(buf) and run the program again. Which line changes, and what does it print?
- Declare q without => null() and print associated(q) before the first assignment. Repeat the run a few times: is the answer stable?
- Call repoint(p, x) after the first nullify and print associated(p, q). What is the answer now?
- Change view => buf(2:3) to view => buf(1:4:2) and print view and size(view). What are the two elements?
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What does
nullify(p)do?Answer
Makes the pointer not associated with anything
The status becomes not associated; the target is untouched.
-
What does
associated(p, q)ask?Answer
Whether both point at the same object
The two-argument form compares associations.
-
What does a
real, pointer, intent(inout) :: ptrdummy receive?Answer
The pointer itself, so it can be re-associated for the caller
That is what the pointer attribute on a dummy is for.
-
What does this program print?
program p implicit none real, target :: x real, pointer :: a => null(), b x = 4.0 a => x b => x nullify(a) print *, associated(a), associated(b), associated(a, b) end program pAnswer
F T F
a is null, b still points at x, and the pair is not shared.
-
What is the association status of a pointer after its target is deallocated?
Answer
Undefined: not associated, not null
The standard leaves it undefined, so any answer is meaningless.
-
Why must a pointer be nullified before its target is deallocated?
Answer
To leave a defined status instead of an undefined one
A null status is checkable; an undefined one is not.
-
What is
view => buf(2:3)?Answer
A pointer to two elements of buf, with its own bounds
The pointer descriptor carries bounds and aliases the storage.
-
What does gfortran report for a plain variable passed to a pointer dummy?
Answer
Error: actual argument must be a pointer or a valid target for the dummy pointer
A variable with TARGET may be passed; a dummy without it may not.
-
What is the status of a pointer declared without an initialiser?
Answer
Undefined, so testing it means nothing
Write => null() and the question has an answer.
-
Which declaration cannot dangle?
Answer
An allocatable
An allocatable owns its storage and frees it with the scope.
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