• Aucun résultat trouvé

Array Specifications

Dans le document The Fortran 2003 Handbook (Page 131-136)

4 Data Types

5.4 Array Properties

5.4.1 Array Specifications

There are four forms that an array specification (R510) may take:

explicit-shape-spec-list assumed-shape-spec-list deferred-shape-spec-list assumed-size-spec

The specified rank is the number of comma-separated items in the array specifica-tion, which is one more than the number of commas. The maximum rank of an array is 7.

5.4.1.1 Explicit-Shape Arrays

An explicit-shape array is one whose bounds are entirely determined from its array specification, which is an explicit-shape specification list. Each dimension has an ex-plicit-shape specification (R514), which has the form:

[ lower-bound : ] upper-bound

where the lower bound, if present, and the upper bound are specification expressions (7.4.2).

Rules and restrictions:

1. If the lower bound is omitted, the default value is 1.

Declarations 125 2. If any of the bound expressions is not an initialization expression, then the array specification must be in the scoping unit of a subprogram or interface body. The expression is evaluated and establishes the bound on each entry to the procedure.

The bound is not affected by any changes to the values of variables in the sion during execution of the procedure. An array declared using such an expres-sion is an automatic variable unless it is a dummy argument or function result.

The subscript range of the array in a given dimension is the set of integer values between and including the lower and upper bounds, provided the upper bound is not less than the lower bound. If the upper bound is less than the lower bound, the range is empty, the extent in that dimension is 0, and the size of the array is 0.

Examples of explicit-shape arrays:

REAL Q (-10:10, -10:10, 2) or in a subroutine

SUBROUTINE EX1 (Z, I, J)

REAL, DIMENSION (2:I + 1, J) :: Z . . .

5.4.1.2 Assumed-Shape Arrays

An assumed-shape array is a dummy argument that takes the shape of the actual argu-ment passed to it. Each dimension has an assumed-shape specification (R514), which has the form:

[ lower-bound ] : Rules and restrictions:

1. An assumed-shape array must be a dummy argument.

2. If the lower bound is omitted, the default value is 1. Note that the lower bound is not assumed from the actual argument; only the shape (extents) is assumed.

3. If the lower bound is specified, it must be a specification expression and it is eval-uated on entry to the procedure. The bound is not affected by any changes to the values of variables in the expression during execution of the procedure.

4. The upper bound is the extent of the corresponding dimension of the associated ar-ray plus the lower bound minus 1.

5. An assumed-shape array cannot have the POINTER or ALLOCATABLE attribute, but this is more a matter of definition than a restriction. Pointer or allocatable dum-my arrays are deferred shape, which has a similar syntax as described below.

126 Chapter 5

Examples of assumed-shape arrays:

SUBROUTINE EX2 (A, B, X)

REAL, DIMENSION (2:, :) :: X REAL, INTENT(IN) :: A(:), B(0:) Suppose EX2 is called by the statement

CALL EX2 ( U, V, W (4:9, 2:6))

For the duration of the execution of subroutine EX2, the dummy argument X is an ar-ray with bounds (2:7, 1:5). The lower bound of the first dimension is 2 because X is de-clared to have a lower bound of 2. The upper bound is 7 because the dummy argument takes its shape from the actual argument W.

5.4.1.3 Deferred-Shape Arrays

A deferred-shape array is one whose bounds may change at times other than entry to a procedure. Each dimension has a deferred-shape specification (R515), which has the form:

:

Rules and restrictions:

1. The array must have either the ALLOCATABLE or POINTER attribute. This is the only form of array specification allowed for allocatables or pointers.

2. The size, bounds, and shape of a disassociated array pointer or unallocated allocat-able array are undefined.

A deferred-shape array may or may not be a dummy argument. If a deferred-shape array is a dummy argument, then on entry to the procedure the definition status and values of the dummy argumentʹs bounds are those of the actual argument; this applies to both the lower and upper bounds and is thus different from assumed-shape arrays, which do not assume the lower bounds. Another important difference between as-sumed-shape and deferred-shape dummy arguments is that a deferred-shape dummy argument can change bounds during execution of the procedure it is in; such changes propagate back to the actual argument on termination of the procedure. The bounds of an assumed-shape dummy argument cannot change during execution of the proce-dure. Details of argument association for deferred-shape dummy arrays are discussed in 12.6.5 and 12.6.7.

The bounds of an allocatable array are determined when it is allocated. Allocation of allocatable variables is discussed in 6.7.1.1. The bounds of a pointer array are deter-mined when it is associated with a target, through allocation or other means, as dis-cussed in 6.7.1.2, 7.5.5.1, and 12.6.5.

Examples of deferred-shape arrays:

REAL, POINTER :: D (:,:), P (:) ! pointer arrays REAL, ALLOCATABLE :: E (:) ! allocatable array

Declarations 127

5.4.1.4 Assumed-Size Arrays

An assumed-size array is a dummy argument array whose size is assumed from that of the associated actual argument. Only the size is assumed—the rank and bounds (ex-cept for the upper bound and extent in the last dimension) are determined from its as-sumed-size array specification (R516) which has the form:

[ explicit-shape-spec-list , ] [ lower-bound : ] *

The form and interpretation of the array specification for an assumed-size dummy ar-ray is identical to that of an explicit-shape arar-ray except for the replacement of the last upper bound by an asterisk.

Rules and restrictions:

1. An assumed-size array must be a dummy argument.

2. If any lower bound is omitted, the default value is 1.

3. Each bound expression must be a specification expression and it is evaluated on entry to the procedure. The bound is not affected by any changes to the values of variables in the expression during execution of the procedure.

4. If an assumed-size array has the INTENT (OUT) attribute, the array must not be of a type that has default initialization.

5. The name of an assumed-size array must not be used as a whole-array reference except as an actual argument in a procedure reference for which the arrayʹs shape is not required.

6. The upper bound and extent of the last dimension of an assumed-size array are not defined.

7. In an array section (6.6.4) of an assumed-size array, the second subscript (upper limit) must not be omitted from a subscript triplet in the last dimension.

Conceptually, the requirements on assumed-size arrays derive from the presump-tion that the compiler will not necessarily “know” the actual array size. The array has a size, as defined below, but this serves only as a definition of the limits that the pro-grammer is required to adhere to. An assumed-size array cannot be used in a context where the compiler would need knowledge of the size. For example, a statement such as

write (*,*) x

requires that the compiler know the size of x in order to write out the appropriate number of values; therefore, this statement is disallowed if x is an assumed-size array.

Similarly, because the extent and upper bound of the last dimension of an as-sumed-size array are not defined, an array slice such as x(3,:) is disallowed. However, a slice such as x(:,3) is allowed.

128 Chapter 5

The size of an assumed-size array is defined as follows:

1. If the actual argument associated with the assumed-size dummy argument is an ar-ray of any type other than default character, the size is that of the actual arar-ray.

2. If the actual argument associated with the assumed-size dummy array is an array element of any type other than default character with a subscript order value (6.6.6) of v in an array of size x, the size of the dummy argument is x − v + 1.

3. If the actual argument is a default character array, default character array element, or a default character array element substring (6.4), and if it begins at character storage unit t of an array with c character storage units, the size of the dummy ar-ray is

MAX (INT ((c − t + 1) / e), 0)

where e is the length of an element in the dummy character array.

This complicated-sounding definition can be stated simply in informal terms: the assumed-size array is big enough to fill to the end of the array in the actual argument;

the filling is done by characters if the type is default character, and by elements other-wise.

Some implementations track the actual size of assumed-size arrays in order to fa-cilitate debugging, but the standard is written so as not to require this.

Examples of assumed-size arrays:

SUBROUTINE EX3 (N, S, Y) REAL, DIMENSION (N, *) :: S REAL Y (10, 5, *)

. . .

5.4.1.5 Limitations on Whole Arrays

There are some limitations on appearances in a program of whole arrays declared with each of the four forms of array specification. Table 5-1 gives a partial summary of the allowable appearances.

Table 5-1 Partial summary of allowable appearances of whole arrays declared in each of the four ways

An array declared with May appear as a

Explicit shape

Assumed shape

Deferred shape

Assumed size

Primary in an expression Yes Yes Yes No

Vector subscript Yes Yes Yes No

Dummy argument Yes Yes Yes Yes

Declarations 129

Dans le document The Fortran 2003 Handbook (Page 131-136)