Fortran 90 has three varieties of dynamic arrays. All three allow array creation at run time with sizes determined by computed (or input) values. These three varieties of dynamic arrays are:
function F18(A,N) integer N ! A scalar real A(:,:) ! An assumed shape array real F18(size(A,1) ) ! The function result itself is ! an automatic array. complex Local_1(N,2*N+3) ! Local_1 is an automatic array ! whose size is based on N. real Local_2(size(A,1),size(A,2)) ! Local_2 is an automatic array ! exactly the same size as A. real Local_3(4*size(A,2)) ! Local_3 is a one-dimensional ! array 4 times the size of ! the second dimension of A. ... ! end function F18Note the importance of the intrinsic inquiry functions, such as SIZE (which returns the argument array size in a specified dimension) in declaring automatic arrays. Fortran 90 provides a number of inquiry functions that are allowed to appear in declarations. Array bounds and sizes, character lengths, and type kinds may all be specified with expressions involving these inquiry functions. Roughly, a specification expression, as such expressions are called, is a scalar integer expression that has operands whose values are determinable upon entry to the procedure. Such operands include constants, references to intrinsic procedures, and variables accessible through dummy arguments, modules, common, and (in the case of module and internal procedures) the host procedure.
subroutine Peach use Recipe ! Accesses global allocatable array, Jam. real, allocable :: Pie(:,:) ! Pie is a 2-dimensional allocatable array. ... allocate ( Pie(N,2*N ) ) ! Allocate a local allocatable array. if (.not.allocated(Jam)) allocate ( Jam(4*M) ) ! Allocate a global allocable array if ! it is not already allocated. ... deallocate ( Pie ) ... end subroutine Peach module Recipe ! Jam is a global allocatable array, and real, allocable :: Jam(:) ! can be allocated and deallocated in ... ! any procedure(s) using this module. end module RecipeNote that the declared bounds for allocatable arrays are simply colons, indicating that these will be provided later, at the time of allocation. This makes allocatable array declaration appear similar to assumed-shape dummy argument declaration, appropriate because the ``deferred" nature of the sizes of the dimensions is conceptually similar.
In addition, pointer arrays can be used as aliases for (``point to") other arrays and array sections; the pointer assignment statement is used to establish such aliases. The target for pointer associations (as such aliasing is called) may be other explicitly allocated arrays, or static or automatic arrays that have been explicitly identified as allowable targets for pointers. The association status of a pointer array may be tested with the ASSOCIATED intrinsic function. Finally, pointer arrays may be dummy arguments and structure components, neither of which are allowed for allocatable arrays. Given this apparent similarity between allocatable arrays and pointer arrays, what is the fundamental distinction between these two forms of dynamic arrays, and when should allocatable arrays be used rather than pointer arrays? Pointer arrays subsume all of the functionality of allocatable arrays, and in this sense allocatable arrays are never needed-pointer arrays could always suffice. The problem with pointer arrays is efficiency. Though pointer arrays must always point to explicit targets, which makes optimization practical that would otherwise be infeasible, pointer assignment makes optimization of pointer arrays much more difficult than for allocatable arrays. Because of their more limited nature and functionality, allocatable arrays are just ``simpler" and can be expected to be more efficient than pointer arrays.
Therefore, when all that is needed is simple dynamic allocation and deallocation of arrays, and automatic arrays are not sufficient, use allocatable arrays. A common example of this is if a ``work array" is needed of a size dependent upon the results of a local computation. If, on the other hand, the algorithm calls for a dynamic alias, of for example a ``moving" section of a host array, then a pointer array is probably indicated.