MadSci Network: Computer Science

Re: What does the statement ALLOCATE means in FORTRAN language?

Date: Thu Mar 16 12:42:02 2000
Posted By: ,
Area of science: Computer Science
ID: 953145046.Cs

The ALLOCATE statement and ALLOCATABLE type qualifier are features 
introduced in Fortran 90. They allow arrays whose size is determined at run 
time rather than at compile time.

In a nutshell, ALLOCATABLE is used as part of a type declaration to tell the 
compiler that the size of the array will be determined later, as the program 
runs. The compiler keeps track of the array name, but no space is actually 
allocated until you use the ALLOCATE statement, which reserves memory for 
the array. It is similar to leaving the last subscript of an array pointer 
unspecified in C, except that Fortran allows you to leave the size of 
multiple subscripts unspecified until run time. For example, you can easily 
create a square matrix whose size is not specified in FORTRAN using this 

   REAL, ALLOCATABLE :: matrix(:,:)

There is no direct equivalent in C, since it is limited to leaving the last 
subscript unspecified.

The ALLOCATE statement sets aside space for the array itself. It is usually 
coded with one or two parameters, although you can actually allocate 
multiple arrays with a single ALLOCATE statement.

The first parameter specifies the size of the array by giving the array name 
and the number of elements in the array. For a multidimensional array, 
separate the size of the subscripts by commas.

The second parameter is optional. If you use it, an error code is stored in 
the variable you list. The error code is zero if the program was able to 
allocate enough memory for the array, and greater than zero if the program 
could not allocate the memory--in which case you canít use the array, of 
course. If an error occurs and you have used an error parameter, the program 
keeps going, allowing you to handle the error on your own. If an error 
occurs and you did not give the error parameter, the program will stop with 
some sort of error.

To allocate a matrix for our previous example based on a parameter passed to 
a subroutine, we could use the statement

   ALLOCATE (matrix(n,n), STAT = errorCode)

Compared to C, ALLOCATE does the same job as malloc(), but it is more 
specialized. malloc() allocates any kind of memory, while ALLOCATE is only 
used to allocate memory for variable size arrays.

There is also a DEALLOCATE statement that frees the memory used by the 
array. You may not see it often in real code, since the array is also 
deallocated automatically when the program block it is in finishes. For 
example, if you ALLOCATE and array in a SUBROUTINE, the array is 
automatically deallocated when you return from that subroutine. Itís 
parameters are the names of one or more arrays separated by commas, with an 
optional STAT error parameter.

Mike Westerfield
Byte Works, Inc.


Current Queue | Current Queue for Computer Science | Computer Science archives

Try the links in the MadSci Library for more information on Computer Science.

MadSci Home | Information | Search | Random Knowledge Generator | MadSci Archives | Mad Library | MAD Labs | MAD FAQs | Ask a ? | Join Us! | Help Support MadSci

MadSci Network,
© 1995-2000. All rights reserved.