Complete Homework 8, Read Chpt. 17 and the Web notes on I/O
Arrays in argument lists; ALLOCATE, ALLOCATABLE, and DEALLOCATE statements; ALLOCATED intrinsic function; automatic arrays
To use this feature you must declare the array ALLOCATABLE, using a simple ":" in the array declaration, to avoid immediate specification of its size. One quick way to do this for an array "a" is:
real, allocatable :: a(:)The colon between the parentheses is mandatory. You can't do something with a specific range like:
real, allocatable :: a(0:ndim)This makes no sense in an array with no determined size. Once your program has determined the size needed for "a", say "length", use the ALLOCATE statement:
allocate ( a(length))When you are done with "a", you have the option to DEALLOCATE it:
deallocate(a)This is really only an option in the main program. If you allocate an array within a subroutine or function but return without deallocating an array, the array becomes "undefined", and the Fortran 90 doesn't guarantee you can retrieve the contents on re-entry to the subprogram. You are also keeping memory associated with your program that you will no longer use. If you are imbedding ALLOCATE statements within conditional structures (e.g. IF,THEN,ELSE), it may not be possible to always predict whether or not a given array is ALLOCATED. You can check to see if the array is allocated with the logical intrinsic function ALLOCATED:
if (allocated(a)) then print *, 'Array A is allocated' endifYou should also know, that if you chose to declare an array as ALLOCATABLE within a subprogram, then that array's name can't appear in the argument list of the SUBROUTINE or FUNCTION statement. The subroutine beginning:
subroutine (b,n) real, allocatable :: b(:) allocate (b(n))will result in a Fortran error.
Examples of ALLOCATE are provided in array3.f, array3a.f, array3b.f, and array3e.f. Be sure to obtain input decks array3a.in and array3b.in before running these problems. The program array3.f is a simple extension of array2.f, containing the use of dynamic allocation. array3a.f scans the data file to determine space requirements, then uses ALLOCATE. array3b.f simply reads a number from the input file specifying the size of arrays.
The program array3e.f shows what happens if you decide to allocate more space to an array after the first ALLOCATE. Fortran 90 forces you to make the size decision once. If gradual expansion of an array is necessary during a program, you will need to use a mixed Fortran - C program, applying the C "malloc" function, use special machine dependent memory functions, or learn to create "linked lists" with the Fortran 90 POINTER data type.
Examples of the use of ALLOCATE within subroutines are given in the program array3as.f (needs input array3a.in). It also contains an example of automatic invocation of ALLOCATE and DEALLOCATE. If an array is declared in a subprogram, using an integer from the argument list as part of its range specification.
subroutine test(n) real a(1:n)but is not in the argument list itself, then it is called an "automatic array". Space for the array is automatically allocated when the subprogram is entered, and deallocated on return from the subprogram.
One word of warning, the simplicity of these statements hides a lot of work by the computer. Use allocation and deallocation sparingly. They will eat up a relatively large amount of computer time. It is a very bad idea to use dynamic allocation within a subroutine or function that is used a large number of times by your program.
Test your knowledge of this material with some review questions.
Written and Maintained by John Mahaffy : firstname.lastname@example.org