Using Fortran allocatable arrays: allocation, use, and safe deallocation
Learn how to declare, allocate, use, and safely deallocate Fortran allocatable arrays, with a complete example, common pitfalls, and verification steps.
21 Jan 2026, 05:49 UTC

Using Fortran allocatable arrays
To change the size of an array during program execution, declare it with the allocatable attribute, then use the allocate statement to bind storage to the desired shape. After the array is no longer needed, call deallocate to return the memory to the system. This three‑step pattern lets you work with arrays whose dimensions are only known at run time.
Example: allocating a two‑dimensional real array
program allocatable_example
implicit none
real, allocatable :: mat(:,:)
integer :: n, m, i, j
n = 100
m = 50
allocate(mat(n,m))
do i = 1, n
do j = 1, m
mat(i,j) = real(i*j)
end do
end do
print *, 'Sum of all elements = ', sum(mat)
deallocate(mat)
end program allocatable_example
The allocate statement creates storage whose bounds match the expressions n and m. If either expression is negative or not an integer, most compilers will abort at runtime; a zero‑size extent is allowed by the standard but some implementations treat it as an error, so verify the size before calling allocate. After deallocate the array becomes undefined; any further reference (including as a dummy argument or in I/O) must be preceded by another allocate that specifies new bounds.
Limits and common mistakes
- Missing the
allocatableattribute in the type declaration leads to a compile‑time error whenallocateis encountered. - Using an unallocated array (e.g., passing it to a subroutine or printing it) causes a runtime error or undefined behavior.
- Calling
allocateon an already allocated array without first deallocating it (unless you use thesource=ormold=options) results in a runtime error. - Providing a shape expression that does not match the declared rank (e.g., allocating a vector with two bounds) triggers a compile‑time mismatch.
- Assuming that deallocation automatically resets the array to a known state; after
deallocatethe array is undefined and must be re‑allocated before use.
Verification and practical checks
Compile the example with checking enabled:
gfortran -fcheck=all -O2 allocatable_example.f90 -o alloc_example
Run the executable; if the program prints a sum and terminates without a runtime error, the allocation and deallocation succeeded. To confirm that the compiler actually emitted calls to the runtime allocation routines, you can inspect the intermediate representation:
gfortran -fdump-tree-all -O2 allocatable_example.f90 2>&1 | grep -i allocate
Look for lines that contain allocate and deallocate in the dump; their presence indicates the statements were not optimized away.
As a quick sanity check, you can add a conditional that prints the shape after allocation:
print *, 'Allocated shape = ', shape(mat)
If the program stops with an allocation error, verify that n and m are non‑negative integers before the allocate statement.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.