| United States-English |
|
|
|
![]() |
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 4 ArraysArray declarations |
|
An array is a data object with the dimension attribute. Its rank—and possibly the extents—are defined by an array specification. The array specification is enclosed in parentheses and can be attached either to the DIMENSION attribute, as in:
or to the array name, as in:
If the array specification is attached both to the DIMENSION attribute and to the array name in the same declaration statement, the specification attached to the name takes precedence. In the following example:
a and b are declared as two-dimensional arrays, but c is declared as a one-dimensional array. An array specification can declare an array as one of the following:
The following sections describe these types and the form of the array specification for each type. For information about initializing arrays with the array constructor, see “Array constructors”. An explicit-shape array has explicitly declared bounds for each dimension; the bounds are neither taken from an actual array argument (“assumed”) nor otherwise specified prior to use (“deferred”). Each dimension of an explicit-shape array has the following form: [lower-bound :] upper-bound where lower-bound and upper-bound are specification expressions and may be positive, negative, or zero. The default for lower-bound is 1. For a given dimension, the values of lower-bound and upper-bound define the range of the array in that dimension. Usually, lower-bound is less than upper-bound; if lower-bound is the same as upper-bound, then the dimension contains only one element; if it is greater, then the dimension contains no elements, the extent of the dimension is zero, and the array is zero-sized. The simplest form is represented by an array declaration in which the name of the array is not a dummy argument and all bounds are constant expressions, as in the following example:
This form of array may have the SAVE attribute and may be declared in any program unit. Other forms of the explicit-shape array include:
Explicit-shape arrays may also be used as function results, as described in “Array-valued functions” and in “Array dummy argument”. The following code segment illustrates different forms of explicit-shape arrays:
An assumed-shape array is a dummy argument that assumes the shape of the corresponding actual argument. It must not have the POINTER attribute. Each dimension of an assumed-shape array has the form: [lower-bound] : where lower-bound is a specification expression. The default for lower-bound is 1. The actual argument and the corresponding dummy argument may have different bounds for each dimension. An assumed-shape array subscript may extend from the specified lower-bound to an upper bound that is equal to lower-bound plus the extent in that dimension of the actual argument minus one. The following code segment illustrates different declarations of assumed-shape arrays.
If a procedure has an argument that is an assumed-shape array, its interface must be explicit within the calling program unit. A procedure’s interface is explicit if it is an internal procedure within the caller procedure or if the interface is declared in an interface block within the caller. For example, to call the external subroutine initialize in the previous example, its interface must appear in an interface block, as in the following:
For more information about:
A deferred-shape array has either the POINTER attribute or the ALLOCATABLE attribute. Its shape is not specified until the array is pointer assigned or allocated. Although a deferred-shape array can have the same form as an assumed-shape array, the two are different. The assumed-shape array is a dummy argument and must not have the POINTER attribute. The array specification for a deferred-shape array has the form:
The specification for a deferred-shape array defines its rank but not the bounds. The bounds are defined either when the array is allocated or when an array pointer becomes associated with a target. Array pointers and allocatable arrays are described in the following sections. An array pointer is a deferred-shape array with the POINTER attribute. Its bounds and shape are defined only when the array is associated with a target in a pointer assignment statement or in an ALLOCATE statement. An array pointer must not be referenced until it is associated. Following are example declarations of array pointers:
For information about associating an array pointer with a target, see “Pointers”. For information about the POINTER attribute and ALLOCATE statement, see Chapter 10, “HP Fortran Statements.” An allocatable array is a deferred-shape array with the ALLOCATABLE attribute. Its bounds and shape are defined when it is allocated with the ALLOCATE statement. Once allocated, the allocatable array may be used in any context in which any other array may appear. An allocatable array can also be deallocated with the DEALLOCATE statement. An allocatable array has an allocation status that can be tested with the ALLOCATED intrinsic inquiry function. Its status is unallocated when the array is first declared and after it is deallocated in a DEALLOCATE statement. After the execution of the ALLOCATE statement, its status is allocated. An allocatable array with the unallocated status may not be referenced except as an argument to the ALLOCATED intrinsic or in an ALLOCATE statement. If it has the allocated status, it may not be referenced in the ALLOCATE statement. It is an error to allocate an allocatable array that is already allocated, or to deallocate an allocatable array either before it is allocated or after it is deallocated. In HP Fortran, an allocatable array that is unallocated, is local to a procedure, and does not have the SAVE attribute. It is automatically deallocated when the procedure exits. The following example, alloc_array.f90, calls a subroutine that allocates and deallocates an allocatable array and uses the ALLOCATED intrinsic function to test its allocation status: Example 4-1 alloc_array.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
For information about the ALLOCATABLE, ALLOCATE, DEALLOCATE statements, see Chapter 10, “HP Fortran Statements.” See also “ALLOCATED(ARRAY)”. An assumed-size array is a dummy argument whose size is taken from the associated actual argument. Its declaration specifies the rank and the extents for each dimension except the last. The extent of the last dimension is represented by an asterisk (*), as in the following:
All dummy array arguments and their corresponding actual arguments share the same initial element and are storage associated. In the case of explicit-shape and assumed-size arrays, the actual and dummy array need not have the same shape or even the same rank. The size of the dummy array, however, must not exceed the size of the actual argument. Therefore, a subscript in the last dimension of an assumed-size array may extend from the lower bound to a value that does not cause the reference to go beyond the storage associated with the actual argument. Because the last dimension of an assumed-size array has no upper bound, the dimension has no extent and the array consequently has no shape. The name of an assumed-size array therefore cannot appear in contexts in which a shape is required, such as a function result or a whole array reference. The following example, assumed_size.f90, illustrates two assumed-size arrays: x (declared in subr) and i_array (declared in func): Example 4-2 assumed_size.f90
Here are the command lines to compile and execute the program, along with the output from a sample run:
An assumed-size array is a FORTRAN 77 feature that has been superseded by the assumed-shape array; see “Assumed-shape arrays”. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||