Pointer or allocatable

Both an allocatable and a pointer can hold an array whose size is decided at run time, and that resemblance is where the trouble starts. They differ in one thing that matters more than the syntax: an allocatable owns its storage, and a pointer does not. This lesson puts the two side by side on the same array, changes a value through each one, and shows the deallocation, the dummy argument and the section that only one of them accepts.

What each one owns

An allocatable variable is the only name for its storage. Assignment copies, deallocation frees, and leaving the scope frees automatically. A pointer is a second name for storage that something else owns: the target may be a variable with the target attribute, another pointer or a section of an array, and the pointer may be re-associated with a different target at any time. When the target dies the pointer does not follow it, and the language does not check.

The file cmp.f90:

program comparedemo implicit none integer, allocatable, target :: a(:) integer, allocatable :: b(:) integer, pointer :: p(:) => null() allocate(a(4)) a = [1, 2, 3, 4] b = a b(1) = 100 print '(a,4i4)', 'a after b=a =', a print '(a,4i4)', 'b =', b p => a p(1) = 7 print '(a,4i4)', 'a after p(1)=7 =', a print '(a,l1)', 'p is associated = ', associated(p) call double_it(b) print '(a,4i4)', 'b after double =', b print '(a,4i4)', 'a still =', a nullify(p) deallocate(a) print '(a,l1)', 'a allocated = ', allocated(a) print '(a,l1)', 'p associated = ', associated(p) contains subroutine double_it(v) integer, allocatable, intent(inout) :: v(:) v = 2 * v end subroutine double_it end program comparedemo

gfortran -Wall -Wextra -std=f2018 cmp.f90 -o cmp
./cmp

a after b=a = 1 2 3 4
b = 100 2 3 4
p is associated = T
a after p(1)=7 = 7 2 3 4
b after double = 200 4 6 8
a still = 7 2 3 4
a allocated = F
p associated = F

A copy and an alias

The first pair of lines is the allocatable: b = a produced a second array, and b(1) = 100 changed only b. The third line is the pointer: p => a produced a second name, and p(1) = 7 changed a. One statement copies, the other shares, and everything that follows from the two declarations follows from that single difference.

The fourth line shows the cost of sharing: a was modified by a statement that never mentions a. Every reader of the program has to know that p aliases a, and no type or name says so. With an allocatable that question cannot arise, which is why this course recommends one for anything that is stored rather than viewed.

The dummy argument forms

integer, allocatable, intent(inout) :: v(:) is a dummy that receives an allocatable whole array, with its allocation status and bounds, and may reallocate it for the caller. The call double_it(b) changed b in place because the shape was already right. A pointer dummy, integer, pointer :: v(:), receives the pointer and may re-associate it, which the allocatable dummy cannot.

A plain assumed-shape dummy, integer, intent(inout) :: v(:), is the form to write when the procedure only reads or writes elements: it accepts an allocatable, a pointer, a section and a fixed-size array, and the compiler passes the address. The allocatable dummy is for the procedures that allocate, deallocate or resize what they receive, which is a much smaller set.

Automatic deallocation

deallocate(a) is explicit here, but it is not required: an allocatable local is freed when its scope ends, an allocatable component is freed with its record, and an allocatable dummy with intent(out) is freed on entry. That is the property that makes an allocatable safe: there is no path through the program that leaves it allocated after its name is gone.

A pointer has none of that. p was nullified before the deallocation in the program, which is the discipline of section 084; without the nullify it would have kept the address of a block that no longer exists, and the next read would be undefined.

A section for an allocatable

A section is a view of an array, not an array of its own, so it cannot be the actual argument of an allocatable dummy:

program badsection implicit none integer, allocatable :: a(:) allocate(a(4)) a = [1, 2, 3, 4] call double_it(a(2:3)) print *, a contains subroutine double_it(v) integer, allocatable, intent(inout) :: v(:) v = 2 * v end subroutine double_it end program badsection

gfortran -Wall -Wextra -std=f2018 badsection.f90 -o badsection

badsection.f90:7:17:

7 | call double_it(a(2:3))
| 1
Error: Actual argument for 'v' must be ALLOCATABLE at (1)

The message is exactly the rule: a section is not allocatable. The fix is the assumed-shape dummy, which accepts the section as a view and writes through it, or a pointer dummy if the procedure needs the pointer itself. That is the practical reason the plain assumed-shape dummy is the default in this course: it is the only form that takes all four kinds of actual argument.

How to choose

Write an allocatable when the variable owns its storage: an array whose size is read from a file, the result of a computation, a component that belongs to its record. It cannot dangle, it frees itself, and assignment is a copy a reader understands. That covers almost everything a program stores.

Write a pointer when a name has to refer to storage that something else owns: a view of a section, a node that links to another node, a callback that may be null. Those are the three reasons of this course, and each of them comes with a lifetime the program has to prove.

The test is one question. If the storage should die with this name, it is an allocatable; if another name will still need it, it is a pointer and the program owes a rule about when the target dies. A variable that passes the test as a pointer and could be an allocatable is a bug waiting for a refactoring.

Exercises

  1. Remove target from a. Which line stops compiling?
  2. Change double_it to take integer, intent(inout) :: v(:) and call it with a(2:3). Does the program compile, and what changes in a?
  3. Point p at a(2:3) and write through it. Which elements of a change?
  4. Take the nullify(p) out of the program and print associated(p) after the deallocation. What is the answer, and what does it prove?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does an assignment between two allocatables do?

    Answer

    Copies the values into storage of its own

    An allocatable owns its storage, so assignment copies.

  2. What does a pointer assignment p => a do?

    Answer

    Makes p a second name for the storage of a

    The arrow shares; the equals sign copies.

  3. What does this program print? program p implicit none integer, allocatable, target :: a(:) integer, pointer :: q(:) => null() allocate(a(3)) a = [1, 2, 3] q => a q(2) = 9 print *, a(2), q(1) end program p

    Answer

    9 1

    q aliases a, so the write is visible in both.

  4. Which one frees its storage automatically?

    Answer

    An allocatable

    A pointer does not own what it points at.

  5. What does an allocatable dummy let a procedure do that a plain dummy does not?

    Answer

    Allocate, deallocate or resize the actual argument

    The allocation status travels with an allocatable dummy.

  6. Which dummy form accepts an allocatable, a pointer, a section and a fixed array?

    Answer

    The plain assumed-shape dummy

    A section is not allocatable, so the allocatable dummy refuses it.

  7. What does gfortran report for call f(a(2:3)) with an allocatable dummy?

    Answer

    Error: actual argument must be ALLOCATABLE

    A section has no allocation status of its own.

  8. When is a pointer the right choice?

    Answer

    When a name must refer to storage another name owns

    That is the ownership test.

  9. What is the risk of the aliasing a pointer creates?

    Answer

    A statement can change a variable it does not mention

    Nothing in the source shows the second name.

  10. Why does a pointer to a deallocated target need nullify beforehand?

    Answer

    Without it the pointer status becomes undefined

    A defined status can be tested; an undefined one cannot.

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