Self-Assessment Quiz: Pointers, Targets, and Dynamic Data Structures

Twenty questions to confirm you can tell aliasing from copying, spot the two pointer hazards, and apply the "prefer allocatable" rule. Aim for 16 or more. Answers and a topic map are at the end — try the whole quiz first.


Question 1

The operator that makes a pointer an alias for a target is: - A. = - B. => - C. == - D. ->

Question 2

For a pointer p associated with a target a, the statement p = 4.0_dp: - A. Makes p point at a new object holding 4.0 - B. Writes the value 4.0 through p into a - C. Is a compile error - D. Disassociates p

Question 3

Before a pointer may point at an object, that object must have which attribute (or itself be a pointer)? - A. allocatable - B. save - C. target - D. parameter

Question 4

Why does the compiler treat an object without the target attribute more favorably for optimization? - A. It can assume no pointer aliases the object, so it may keep it in a register and reorder freely - B. Non-target objects use less memory - C. Non-target objects are always contiguous - D. It disables bounds checking

Question 5

The three association statuses of a pointer are: - A. allocated, deallocated, moved - B. associated, disassociated, undefined - C. null, valid, invalid - D. open, closed, pending

Question 6

Calling associated(p) on a pointer that was declared but never initialized is: - A. Guaranteed to return .false. - B. Guaranteed to return .true. - C. Undefined behavior — you must not do it - D. A compile error

Question 7

The recommended habit that prevents undefined pointers is: - A. Always call deallocate first - B. Initialize every pointer => null() (or nullify it) before first use - C. Declare pointers save - D. Never use more than one pointer

Question 8

A dangling pointer is one that: - A. Has never been given a status - B. Points at freed or out-of-scope memory while possibly still reporting associated - C. Points at a contiguous array - D. Has the target attribute

Question 9

After deallocate(p) where q => p had aliased the same memory, the correct action for q is: - A. Nothing — q is fine - B. deallocate(q) as well - C. nullify(q) and never dereference it again - D. q = 0.0_dp

Question 10

In destroy of a linked list, why must you save p%next before deallocate(p)? - A. To make the loop shorter - B. Because after deallocation the node's next field is gone, so reading it is a dangling access - C. Because deallocate needs the next pointer - D. To avoid a compile error

Question 11

In Fortran, compared with a pointer-linked list, a contiguous array is usually faster to sweep because: - A. It uses floating-point instead of integer arithmetic - B. Its predictable, adjacent memory lets the CPU prefetch and the compiler vectorize - C. Arrays skip the arithmetic entirely - D. Linked lists cannot store numbers

Question 12

Which is not an advantage of allocatable over pointer? - A. Automatic deallocation on scope exit - B. No possibility of dangling - C. The ability to point at an already-existing object - D. Full no-aliasing optimization freedom

Question 13

For two allocatable arrays, b = a (with a allocated and larger than b): - A. Is an error unless they are the same size - B. Deep-copies a into b, reallocating b to a's shape automatically - C. Makes b alias a - D. Copies only the first element

Question 14

Which situation genuinely requires a pointer rather than an allocatable? - A. A local scratch array sized at run time - B. A run-time-sized array bundled in a derived type - C. Converting a C library's type(c_ptr) handle into a usable Fortran array - D. Summing a large dataset

Question 15

A procedure, pointer is used to: - A. Point at a block of allocated data - B. Store which procedure to call, so one call site can invoke different behaviors - C. Make a subroutine run faster - D. Alias a derived-type component

Question 16

The contiguous attribute on an array pointer or dummy argument is: - A. A promise that the array is one unbroken block of memory, licensing optimization - B. A command that copies the array to make it contiguous - C. A synonym for allocatable - D. A way to disable vectorization

Question 17

In column-major storage, which section is contiguous? - A. A row, a(i,:) - B. A column, a(:,j) - C. Neither - D. Both

Question 18

The intrinsic that reports, at run time, whether an array actually occupies contiguous memory is: - A. associated - B. allocated - C. is_contiguous - D. contiguous

Question 19

The heat solver's field_t stores its grid as real(dp), allocatable :: u(:,:) rather than a pointer component chiefly because: - A. Pointers cannot store real numbers - B. Automatic deallocation, no dangling, and no aliasing (so the stencil optimizes well) - C. Allocatable arrays are always smaller - D. The standard forbids pointer components

Question 20

What does this print?

real(dp), target  :: a = 1.0_dp, b = 2.0_dp
real(dp), pointer :: p => null()
p => a
p => b
p = 8.0_dp
print '(2f6.1)', a, b
  • A. 8.0 2.0
  • B. 1.0 8.0
  • C. 8.0 8.0
  • D. 1.0 2.0

Answer Key

Q Ans Why
1 B => is pointer assignment (aliasing); = is value assignment.
2 B On an associated pointer, = writes the value through into the target.
3 C Only a target (or another pointer) may be a pointer's target.
4 A No-target ⇒ no aliasing assumed ⇒ registers and reordering are safe.
5 B Associated, disassociated, undefined.
6 C associated on an undefined pointer is undefined behavior.
7 B Give every pointer a defined status at birth via => null() / nullify.
8 B Dangling = aliases freed/vanished memory, may still look associated.
9 C Nullify the surviving alias; never dereference freed memory.
10 B After deallocate(p) the next field is gone; save it first.
11 B Contiguity enables prefetching and vectorization; pointer-chasing does neither.
12 C Only a pointer can alias existing data — that is a pointer advantage, not allocatable's.
13 B Allocatable assignment deep-copies and auto-(re)allocates the LHS.
14 C A C type(c_ptr) handle must become a Fortran pointer (c_f_pointer).
15 B A procedure pointer selects which procedure to call at run time.
16 A It is a promise of contiguity that licenses (does not force) optimization.
17 B First index varies fastest, so a whole column is one unbroken run.
18 C is_contiguous tests contiguity at run time.
19 B Auto-deallocation, no dangling, and no aliasing → a well-optimized stencil.
20 B p ends aliasing b; p = 8 writes 8 into b; a stays 1.0 → 1.0 8.0.

Topics to review by question

  • Q1–4 → §11.1 (pointer/target, => vs =, why target affects optimization).
  • Q5–10 → §11.2 (association status; undefined and dangling hazards; safe list teardown).
  • Q11 → §11.3 (why arrays usually win in Fortran).
  • Q12–13, 19 → §11.4 (allocatable vs pointer; assignment semantics; the solver's field).
  • Q14–16, 18 → §11.5 (when pointers are required; procedure pointers; contiguous/is_contiguous).
  • Q17 → §11.5 and Chapter 5 (column-major contiguity).
  • Q20 → §11.1 (the => moves the alias, = writes the value distinction — the whole chapter in one item).

Scored below 16? Questions 1–2 and 20 are the load-bearing ones: if => versus = is not yet automatic, reread §11.1 and retype code/example-01-pointer-basics.f90 before moving on.