Fortran Pointers & Targets
Fortran pointers are quite different from C pointers. A Fortran pointer is an alias: a name that can be associated with a variable (or an array section) and then used exactly like it. There is no pointer arithmetic and no explicit dereferencing. Most code doesn't need pointers at all, since allocatable arrays cover dynamic memory, but they are essential for linked data structures and for aliasing parts of arrays.
pointer and target
A pointer can only point at a variable that has the target attribute (or at another pointer). Pointer assignment uses =>; normal assignment = copies values through the pointer.
program pointer_basics
implicit none
integer, target :: a = 10, b = 20
integer, pointer :: p => null()
print '(a, l1)', 'associated? ', associated(p)
p => a ! p now aliases a
print '(a, i0)', 'p = ', p
p = 99 ! assigns through the pointer: changes a
print '(a, i0)', 'a = ', a
p => b ! re-point at b
p = p + 1
print '(a, i0, a, i0)', 'a = ', a, ', b = ', b
print '(a, l1)', 'p points at b? ', associated(p, b)
nullify(p) ! disassociate
print '(a, l1)', 'associated? ', associated(p)
end program pointer_basicsassociated? F
p = 10
a = 99
a = 99, b = 21
p points at b? T
associated? F
| Syntax | Meaning |
|---|---|
integer, pointer :: p => null() |
declare a pointer, initially disassociated |
p => a |
pointer assignment: p becomes an alias for a |
p = 5 |
ordinary assignment: stores 5 into whatever p points at |
associated(p) / associated(p, a) |
is p associated (with a)? |
nullify(p) |
make p point at nothing |
Tip: Always initialise pointers with
=> null(). An uninitialised pointer has an undefined status, and evenassociated(p)on it is not allowed.
Pointers to array sections
A pointer can alias any section of an array, even a strided one, and then be used as a normal array with its own bounds starting at 1:
program section_pointers
implicit none
integer :: i
real, target :: grid(4, 4)
real, pointer :: row(:), col(:), diag_block(:, :)
grid = reshape([(real(i), i = 1, 16)], [4, 4])
row => grid(2, :) ! second row
col => grid(:, 3) ! third column
diag_block => grid(3:4, 3:4) ! lower-right 2x2 block
print '(a, 4f5.1)', 'row 2: ', row
print '(a, 4f5.1)', 'col 3: ', col
diag_block = 0.0 ! writes into grid
do i = 1, 4
print '(4f5.1)', grid(i, :)
end do
end program section_pointersrow 2: 2.0 6.0 10.0 14.0
col 3: 9.0 10.0 11.0 12.0
1.0 5.0 9.0 13.0
2.0 6.0 10.0 14.0
3.0 7.0 0.0 0.0
4.0 8.0 0.0 0.0
Allocating through pointers
A pointer can also own memory of its own with allocate. Unlike allocatable variables, that memory is not freed automatically: you must deallocate it, and if you re-point the pointer first, the memory leaks. This is the main reason to prefer allocatable for plain dynamic arrays.
real, pointer :: buf(:) => null()
allocate(buf(1000))
...
deallocate(buf) ! required, or the memory leaksA linked list
Pointers become necessary for recursive data structures, where a type refers to itself. Here's a small singly linked list:
module linked_list
implicit none
type :: node
integer :: value
type(node), pointer :: next => null()
end type node
contains
subroutine push_front(head, v)
type(node), pointer, intent(inout) :: head
integer, intent(in) :: v
type(node), pointer :: new
allocate(new)
new%value = v
new%next => head
head => new
end subroutine push_front
subroutine print_list(head)
type(node), pointer, intent(in) :: head
type(node), pointer :: cur
cur => head
do while (associated(cur))
write(*, '(i0, a)', advance='no') cur%value, ' -> '
cur => cur%next
end do
print '(a)', 'null'
end subroutine print_list
subroutine free_list(head)
type(node), pointer, intent(inout) :: head
type(node), pointer :: tmp
do while (associated(head))
tmp => head%next
deallocate(head)
head => tmp
end do
end subroutine free_list
end module linked_list
program list_demo
use linked_list
implicit none
type(node), pointer :: head => null()
integer :: k
do k = 1, 5
call push_front(head, k * 10)
end do
call print_list(head)
call free_list(head)
print '(a, l1)', 'list empty: ', .not. associated(head)
end program list_demo50 -> 40 -> 30 -> 20 -> 10 -> null
list empty: T
Pointer pitfalls
- Dangling pointers: if the target goes away (a local variable after its procedure returns, or memory deallocated through another pointer), the pointer still "points" at it, and using it is undefined behaviour.
- Aliasing costs performance: because a pointer may alias other data, the compiler optimises pointer code less aggressively than code with plain or allocatable arrays.
=vs=>: forgetting the>copies values instead of re-pointing, a quiet and common bug.
Note:
allocatablecomponents can also build recursive structures without manual memory management (type(node), allocatable :: next), which is safer for trees and lists that are only grown and freed as a whole.
Test yourself
Which statement makes pointer p an alias for variable x?
p = xp => xp = &xp := x
What attribute must a variable have to be pointed at?
savetargetallocatablevalue
Why is allocatable usually preferred over pointer for dynamic arrays?
- Allocatables are freed automatically and can't alias, so they're safer and faster
- Pointers can't be allocated
- Allocatables can be larger
- Pointers are deprecated