• Aucun résultat trouvé

The COMMON Statement

Dans le document The Fortran 2003 Handbook (Page 172-177)

4 Data Types

5.14 Storage Association

5.14.2 The COMMON Statement

The COMMON statement establishes blocks of storage called common blocks and specifies objects that are contained in the blocks. Two or more program units may share this space and thus share the values of variables stored in the space. Thus, the COMMON statement provides a global data facility based on storage association. A common block may be named, in which case it is called a named common block, or may be unnamed, in which case it is called blank common.

A common block may contain a mixture of storage units and may contain unspeci-fied storage units; however, if a common block contains a mixture of storage units, ev-ery declaration of the common block in the program must contain the same sequence of storage units. The form of the COMMON statement (R557) is:

COMMON [ / [ common-block-name ] / ] common-block-object-list &

[ [ , ] / [ common-block-name ] / common-block-object-list ] ...

A(1:1) A(2:2) A(3:3) A(4:4)

B(2:2) B(3:3) B(4:4) C(1)(1:1) C(1)(2:2) C(1)(3:3) C(2)(1:1) C(2)(2:2) C(2)(3:3)

B(1:1)

X(1) X(2) X(3) X(4) X(5) X(6)

Y(1) Y(2) Y(3) Y(4) Y(5) Y(6)

166 Chapter 5

where a common block object (R558) is one of:

variable-name [ ( explicit-shape-spec-list ) ] procedure-pointer-name

Rules and restrictions:

1. A common block object must not be:

a dummy argument an allocatable variable

a structure with an allocatable ultimate component an automatic object

a function name, result name, or entry name a variable with the BIND attribute

accessed by use association

2. If a common block object is of derived type, the type must either be a sequence type or a type with the BIND attribute. In either case, the type must have no de-fault initialization.

3. The object list following a common block name declares objects in the common block of that name. The object list following two slashes with no common block name between them or an object list with no preceding slashes declares objects in blank common.

4. A common block name or an indication of blank common may appear more than once in one or more COMMON statements in the same scoping unit. The object list following each successive block name or blank common indication is treated as a continuation of the previous object list.

5. An object may appear in only one common block within a scoping unit.

6. The DIMENSION attribute for an array can be declared by an explicit-shape speci-fication list in a COMMON statement. Alternatively, the DIMENSION attribute for a variable in common may be specified by other statements as described in 5.4.2.

Pointer arrays are allowed in common, but because they must have a deferred shape, their DIMENSION attribute must be specified by other statements. (The rea-son for this restriction is not clear; it might be accidental.) Because automatic ob-jects are not allowed in common, each bound in the explicit-shape specification list must be an initialization expression.

7. A nonpointer object of type default integer, default real, double precision real, de-fault complex, dede-fault logical, or numeric sequence type must become associated only with nonpointer objects of these types.

8. A nonpointer object of type default character or character sequence must become associated only with nonpointer objects of these types.

Declarations 167 9. A nonpointer object of a type and kind not listed in the previous two rules must become associated only with nonpointer objects of the same type and type param-eter values.

10. A pointer must become storage associated only with pointers of the same type, type parameters, and rank.

11. An object with the TARGET attribute must become storage associated only with objects that have the TARGET attribute and the same type and type parameters.

For each common block, a common block storage sequence is formed. It consists of the sequence of storage units of all the variables listed for the common block in the or-der of their appearance in the common block list. The storage sequence may be extend-ed on the end to include the storage units of any variable equivalencextend-ed to a variable in the common block. Similar extension at the beginning is not allowed. Data objects that are storage associated with a variable in a common block are considered to be in that common block.

The following examples illustrate the distinction between extension at the end and the beginning.

COMMON A(5) REAL B(5)

EQUIVALENCE (A(2), B(1))

is legal and results in the following alignment::

On the other hand, the following is not legal:

EQUIVALENCE (A(1), B(2))

because it would place B(1) ahead of A(1) as in the following alignment:

and a common block must not be extended from the beginning of the block.

Equivalence association must not cause two different common blocks to become as-sociated.

A(1) A(2) A(3) A(4) A(5)

B(1) B(2) B(3) B(4) B(5)

A(1) A(2) A(3) A(4) A(5)

B(1) B(2) B(3) B(4) B(5)

168 Chapter 5 The size of a common block is the size of its storage sequence including any exten-sions of the sequence resulting from equivalence association.

Zero-sized common blocks are permitted. Frequently a program is written with ar-ray extents and character lengths specified by named constants. When there is a need for a different-sized data configuration, the values of the named constants can be changed and the program recompiled. Allowing extents and lengths to be specified to have the value zero, and thus possibly specifying zero-length common blocks, permits the maximum generality.

Within a program, all named common blocks with the same name must have the same size. If that size is zero, the common blocks with that name are associated with one another. If that size is nonzero, the storage sequences of the common blocks with that name all have the same first storage unit. Note that the storage sequence association does not depend on the variable names.

Corresponding rules apply to blank common blocks except that they may be of dif-ferent sizes. Thus, it is possible for a zero-sized blank common block in one scoping unit to be associated with the first storage unit of a nonzero-sized blank common block in another scoping unit.

A blank common block has the same properties as a named common block except for the following:

1. Variables with explicit initialization are allowed in named common in a block data program unit. Variables with explicit initialization are never allowed in blank com-mon.

2. Blank common is, in effect, always saved, even though it cannot be specified in a SAVE statement and the standard does not use that terminology. A named com-mon block is not saved unless it is mentioned in a SAVE statement.

3. Named common blocks of the same name must be the same size in all scoping units of a program. Blank common blocks may be of different sizes.

The following is an example of common block usage:

SUBROUTINE FIRST

INTEGER, PARAMETER :: SHORT = 2 REAL B(2)

COMPLEX C LOGICAL FLAG TYPE COORDINATES

SEQUENCE REAL X, Y LOGICAL Z_O END TYPE COORDINATES TYPE (COORDINATES) P

COMMON / REUSE / B, C, FLAG, P

Declarations 169 REAL MY_VALUES (100)

CHARACTER (LEN = 20) EXPLANATION

COMMON / SHARE / MY_VALUES, EXPLANATION SAVE / SHARE /

REAL, POINTER :: W (:, :)

REAL, TARGET, DIMENSION (100, 100) :: EITHER, OR INTEGER (SHORT) :: M (2000)

COMMON / MIXED / W, EITHER, OR, M . . .

SUBROUTINE SECOND

INTEGER, PARAMETER :: SHORT = 2 INTEGER I(8)

COMMON / REUSE / I REAL MY_VALUES (100)

CHARACTER (LEN = 20) EXPLANATION

COMMON / SHARE / MY_VALUES, EXPLANATION SAVE / SHARE /

REAL, POINTER :: V (:)

REAL, TARGET, DIMENSION (10000) :: ONE, ANOTHER INTEGER (SHORT) :: M (2000)

COMMON / MIXED / V, ONE, ANOTHER, M ! ILLEGAL . . .

Common block REUSE has a storage sequence of 8 numeric storage units. It is be-ing used to conserve storage. The storage referenced in subroutine FIRST is associated with the storage referenced in subroutine SECOND as shown below:

There is no guarantee that the storage is actually retained and reused because, in the absence of a SAVE attribute for REUSE, some processors may release the storage when either of the subroutines completes execution.

Common block SHARE contains both numeric and character storage units and is being used to share data between subroutines FIRST and SECOND.

The declaration of common block MIXED in subroutine SECOND is illegal because it does not have the same sequence of storage units as the declaration of MIXED in subroutine FIRST. The array pointer in FIRST has two dimensions; the array pointer in SECOND has only one. Pointers must match in type, kind and rank in order to have the same storage units.

B(1) B(2) C FLAG X Y Z_O

I(1) I(2) I(3) I(4) I(5) I(6) I(7) I(8)

Dans le document The Fortran 2003 Handbook (Page 172-177)