The Allocate statement is used to assign dimension lengths to arrays that have previously been declared allocatable. By assigning dimensions to an allocatable array with this statement, the programmer opens the array up for use in the program. Note that an array needs to be allocated only once. If one tries to allocate an array that has been previously allocated (and not subsequently deallocated), an error condition occurs and the array becomes undefined.
Another use of the Allocate statement is to create pointer arrays. These allocated pointer arrays do not have to be pointing to the memory space reserved for a target array, but can point to memory space that was specifically created just for use by the pointer. The power behind these pointer arrays is that space can be dynamically allocated for an array when needed and then deallocated when it is no longer needed. This deallocated pointer could then be used in the same capacity, or used to point to an actual array within the program. However there are some things to be careful of when using the allocate statement to create space for a pointer array. First, if the pointer was previously associated with a target array, then the association with that target is broken. Second, if the previous association was as a dynamically allocated pointer array, then the memory space allocated for that pointer array becomes inaccessible to the program, unless another pointer was associated with that block of memory prior to the reallocation of the pointer. All inaccessible space is eventually freed when your program ceases execution.
There is a way to prevent blocks of memory from becoming inaccessible when an array pointer is reallocated. This involves the use of the deallocate statement. When a dynamically allocated pointer array is deallocated, it frees up the memory for later reuse by the program. Therefore, if dynamically allocated pointer arrays are deallocated before they are reallocated, then this will prevent blocks of memory from becoming inaccessible while your program continues to execute.
To use the allocate statement to assign dimensions to an allocatable array:
real, allocatable :: a(:,:) integer n,m n=4 m=5 allocate (a(n,m))
To use the allocate statement with pointers:
101 real, dimension(:,:), pointer :: b,c 102 allocate (b(4,4)) 103 c => b
The array created in this example by allocating the pointer array b is obviously a four by four matrix. The advantage to this type of array is that it can created when needed and then deallocated when it is no longer needed. This can make memory space for future storage of other information.
Note that the array pointer c is set to point to the same memory space as the pointer b. This will prevent the loss of memory space to the program if b is set to point to another array later on in the program. If c was not set to point to the same location as b, the only way to prevent the loss of memory space would be by deallocating b before its use as a pointer elsewhere.
examples: module.f and fall1.f
Written by Jason Wehr: firstname.lastname@example.org and Maintained by John Mahaffy : email@example.com