Allocatable

Attribute Purpose

The Allocatable attribute was born with the FORTRAN 90 standard. It is associated with Type Statements and gives programmers the ability to declare the size of arrays after the program begins executing. This means that programmers don't have to guess at what size arrays will be needed in the application when they start writing code. Instead, the array sizes can be custom fit to the application after the program has begun running. One advantage to using this statement is that it can save space in memory. A custom sized array won't have any unused elements that take up memory space that could be better used elsewhere. Although, be aware that the process of allocating and deallocating arrays can increase the amount of time it takes to execute a program. When these statements are used, there is a lot of shifting of memory going on in the background. This shifting takes relatively large amounts of time. If several large arrays are frequently allocated and deallocated in the program, the time can really add up.

Usage

There are two valid ways in which arrays can be declared allocatable in FORTRAN. This can be done by typing
  		Real, allocatable :: a(:,:)
or
		Real, allocatable, dimension(:,:) :: a 
The first way is recommended just because it is usually a shorter line of code to write, and provides direct visual association of an array's name with its rank. Both styles do the exact same thing. Notice that after the array a is declared allocatable ( that is it has no size associated with its dimensions yet ) the curious construct of having two colons separated by a comma appear in both lines of code. In these statements, each colon is a kind of place holder. The number of colons denote the rank or the number of dimensions that an array will have. The number of elements for each rank is decided later with an Allocate statement. Hence, if the array storage needed to be an allocatable array with a rank of three, it would be declared like this:
		real, allocatable :: storage(:,:,:)
There are two other things that programmers should be aware of when using the allocatable statement. First, do not allocate the dimensions of the same array more than once in a program. The result will either be a compiler error or valuable information that was stored in an array may be overwritten or erased when it is allocated for the second time. Second, do not put a specific dimension length in an allocatable statement. A declaration like this:
		real, allocatable :: a(1:ldata)
makes no sense, and results in a compilation error.

For more information and examples see:

lecture twenty

examples: fall1.f and module.f

Up one level / Home


Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu