Allocatable arrays

Contents
Allocate
Query shape
Move_alloc
Automatic reallocation

Allocate

An allocatable array starts without storage, and the allocate statement reserves the requested size only when the program knows it.

Always pair allocation with deallocation when the array is no longer needed, which keeps long running programs lean.

The program below reserves ten elements, fills them, and then releases the storage.

program demo implicit none real, allocatable :: v(:) allocate(v(10)) v = 0.0 print *, size(v) deallocate(v) end program demo

Query shape

Intrinsics report everything about an array without changing it: size counts the elements, while lbound and ubound report the index range.

The allocated intrinsic tells whether storage currently exists, which guards code that may receive an empty array.

This fragment prints the size and bounds of a small vector.

program demo implicit none real, allocatable :: v(:) allocate(v(4)) if (allocated(v)) print *, size(v) print *, lbound(v), ubound(v) deallocate(v) end program demo

Move_alloc

The move_alloc routine transfers storage from one allocatable to another without copying the elements.

After the move the source is deallocated and the destination owns the memory, which makes growing buffers cheap.

Here the data built in tmp becomes the final array v in one efficient step.

program demo implicit none real, allocatable :: v(:), tmp(:) allocate(tmp(3)) tmp = [1.0, 2.0, 3.0] call move_alloc(tmp, v) print *, v end program demo

Automatic reallocation

Assignment to an unallocated allocatable reserves exactly the needed size, and assignment with a new shape resizes it on the spot.

This rule removes most manual allocate calls for results of expressions and array constructors.

The example assigns two vectors of different lengths in a row, and the array follows along each time.

program demo implicit none real, allocatable :: v(:) v = [1.0, 2.0] print *, size(v) v = [1.0, 2.0, 3.0, 4.0] print *, size(v) end program demo

Next: Pointers in Fortran

Article author: Arthur Isaev

Related articles
Fortran
Fortran course: zero to hero in 50 lessons
What is Fortran and where it runs
Install gfortran
Your first program
Program structure
Types: integer real complex logical character
Kind parameters
Operators
Conditions
DO loops
Arrays
Array sections
Character strings
Formatted IO
Files
Subroutines and functions
Modules
USE with ONLY
INTENT
OPTIONAL and PRESENT
Explicit interfaces
Derived types
Type-bound procedures
Object orientation
Operator overloading
Finalization
Allocatable arrays
Pointers in Fortran
DO CONCURRENT
Coarrays
OpenMP basics
Namelists
IEEE arithmetic
Intrinsic matrix math
Random numbers
String tools
Command-line arguments
Environment variables
error stop
Testing
Build with gfortran and make
Debugging
C interoperability
From fixed form to free form
Numeric models
Performance
Modern Fortran tour: 2008, 2018, 2023
Capstone: stats tool
Capstone: 1D heat equation
Style guide
Hero roadmap: the whole course on one page
Install gfortran on Windows 11
Install gfortran on Ubuntu
Install gfortran on Rocky Linux
Install gfortran on macOS
Install gfortran on FreeBSD
Fortran in Docker

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: