Last modified on 22 August 2012, at 13:05

Fortran/memory management

IntroductionEdit

Most Fortran programs use self-contained data, without structures, and without much in the way of shared, structured data. However, it is possible to share data, in structured and unstructured ways, using common blocks. There's also typically very little memory management going on in a Fortran program. Until the most recent versions, allocated storage wasn't even possible.

Common BlocksEdit

A common block is Fortran's way of using shared, common storage. In its simplest form, a common block is a way of defining global memory. Be careful, though. In most languages, each item in common memory is shared as a globally known name separately. In Fortran, however, the common block is a shared thing. I'll show several examples, but each example will share i and anotherInteger, and myArray, a 10x10 array of real numbers.

In C, for instance, I can define the shared memory using:

 int i;
 int anotherInteger;
 float myArray[10][10];

and use these data elsewhere with:

 extern float myArray[10][10];
 extern int i;
 extern int anotherInteger;

Note that one module declares the storage, and another uses the storage. Also note that the definitions and usages are not in the same order. This is because in C, as in most languages, i, anotherInteger, and myArray are all shared items. Not so in Fortran. In Fortran, all routines sharing this storage would have a definition something like this:

 COMMON i, anotherInteger, myArray
 INTEGER anotherInteger
 REAL myArray(10,10)

This common block is stored as a block of data, as a linkable named structure. The only problem is that we don't know its name. Various compilers will give various names to this block. In some systems, the block actually doesn't have a name. We can avoid this problem by giving the structure a name. For instance,

 COMMON/myBlock/ i, anotherInteger, myArray
 INTEGER anotherInteger
 REAL myArray(10,10)

Using this form, two different Fortran programs can identify the same area of storage and share it, without having to know the structure of all shared storage. Also using this format, a C or other program could share the storage. For instance, a C program wanting to share this storage would declare the same storage as follows:

 extern struct {
    int i;
    int anotherInteger;
    float myArray[10][10];
 } myBlock;

In the above example, having the myBlock names match is critical, as well as having the types, sizes, and order match. However, having the names internally match is not since these names are known only locally. Also note that in the above example, Fortran's myArray(i,j) matches C's myBlock.myArray[j][i].

Memory ManagementEdit

You can declare an array to have a known number of dimensions, but an unknown size using allocation:

 REAL, DIMENSION(:,:), ALLOCATABLE :: myArray
 ...
 ALLOCATE(myArray(10,10))
 ...
 DEALLOCATE(myArray)

You can also declare something as a pointer:

 REAL, DIMENSION(:,:), POINTER :: somePointer
 ...
 ALLOCATE(somePointer(10,10))
 ...
 DEALLOCATE(somePointer)

In archaic versions of Fortran (77 and before), you'd just have a big static array and use whatever portion of it you need.