next up previous

4.3 Dynamic Arrays     continued...

Pointer Arrays
Pointer arrays are similar to allocatable arrays in that they are explicitly allocated with the ALLOCATE statement to have arbitrary computed sizes and are explicitly deallocated with the DEALLOCATE statement. Examples of pointer arrays are given in section 1, in the subsection entitled Pointers. These examples also illustrate target arrays and the use of pointer assignment, the latter of which cannot be used with allocatable arrays. Additional, very simple, examples of pointer arrays result by replacing ``allocatable" with ``pointer" in the preceding examples of allocatable arrays.

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.