| Fortran Language Reference Manual, Volume 1 - S-3692-51 | ||
|---|---|---|
| Prev Section | Chapter 5. Declarations | Next Section |
Generally, the physical storage units or storage order for data objects cannot be specified. However, the COMMON, EQUIVALENCE, and SEQUENCE statements provide sufficient control over the order and layout of storage units to permit data to share storage units.
The COMMON statement provides a means of sharing data between program units. The EQUIVALENCE statement provides a means whereby two or more objects can share the same storage units.
Fortran modules, pointers, allocatable arrays, and automatic data objects provide additional tools for sharing data and managing storage. The SEQUENCE statement defines a storage order for structures. This permits structures to appear in common blocks and be equivalenced. The SEQUENCE statement can appear only in derived-type definitions to define sequence types. The components of a sequence type have an order in storage sequences that is the order of their appearance in the type definition.
Fortran includes numeric and character storage units for numeric and character data. The nondefault types (user-defined types and pointers), however, are stored in unspecified storage units. These unspecified storage units are used for pointers, objects of nondefault type, and structures that contain components that are of nondefault types or are pointers. This unit is different for each different sort of object. A pointer occupies a single unspecified storage unit that is different from that of any nonpointer object and can be different for each combination of type, type parameters, and rank.
There are two kinds of structures, sequence structures and nonsequence structures, depending on whether or not the type definition contains a SEQUENCE statement. A nonsequence structure occupies a single unspecified storage unit that is different for each type. The three kinds of sequence structures are as follows:
Numeric sequence structures (containing only numeric and logical entities of default kind)
Character sequence structures (containing only character entities)
Sequence structures (containing a mixture of components including objects that occupy numeric, character, and unspecified storage units)
Table 5-27 lists objects of various types and attributes and the storage units they occupy.
Table 5-27. Types, Attributes, and Storage
Types and attributes of object | Storage units |
|---|---|
Default integer | 1 numeric |
Default real | 1 numeric |
Logical | 1 numeric |
Double precision | 2 numeric |
Default complex | 2 numeric |
Character of length 1 | 1 character |
Character of length s | s characters |
Nondefault integer | 1 unspecified |
Real other than default real or double precision | 1 unspecified |
Nondefault complex | 1 unspecified |
Nonsequence structure | 1 unspecified |
Numeric sequence structure | n numeric, where n is the number of numeric storage units the structure occupies |
Character sequence structure | n characters, where n is the number of character storage units the structure occupies |
Sequence structure | The sum of the storage sequences of all the ultimate components of the structure. |
Any type with the POINTER attribute | 1 unspecified |
Any intrinsic or sequence type with the DIMENSION attribute | The size of the array times the number of storage units for the type (will appear in array element order) |
Any nonintrinsic or nonsequence type with the DIMENSION attribute | One unspecified storage unit for each element of the array |
Any type with the POINTER attribute and the DIMENSION attribute | 1 unspecified |
A storage sequence is an ordered sequence of storage units. The storage units can be elements in an array, characters in a character variable, components in a sequence structure, or variables in a common block. A sequence of storage sequences forms a composite storage sequence. The order of the storage units in such a composite sequence is the order of the units in each constituent taken in succession, ignoring any zero-sized sequences.
Storage is associated when the storage sequences of two different objects have some storage in common. This permits two or more variables to share the same storage. Two objects are totally associated if they have the same storage sequence; two objects are partially associated if they share some storage but are not totally associated.
To indicate that two or more variables will share storage, they can be placed in an equivalence group in an EQUIVALENCE statement. If the objects in an equivalence group have different types or type parameters, no conversion or mathematical relationship is implied. If a scalar and an array are equivalenced, the scalar does not have array properties and the array does not have the properties of a scalar. The format of the EQUIVALENCE statement is defined as follows:
Table 5-28.
| equivalence_stmt | is |
| |
| equivalence_set | is |
| |
| equivalence_object | is |
| |
|
| or |
| |
|
| or |
|
An equivalence object must not be one of the following items:
A dummy argument
A Fortran pointer
A variable with the ALLOCATABLE attribute
A structure containing a pointer or allocatable component at any level
An automatic data object
A function name, result name, or entry name
A named constant
A structure component
A Cray pointee
An object made accessible by USE association.
A subobject of any of the preceding.
An object with the TARGET attribute.
An object with the BIND attribute.
A variable in a COMMON block that has the BIND attribute.
An equivalence group list must contain at least two items.
Subscripts and substring ranges must be integer initialization expressions. A substring cannot have a length of zero.
If an equivalence object is of type default integer, default real, double-precision real, default complex, default logical, or numeric sequence type, all objects in the set must be of these types.
If an equivalence object is of type character or character sequence type, all objects in the set must be type character. The lengths do not need to be the same.
If an equivalence object is of sequence type other than numeric or character sequence type, all objects in the set must be of the same type.
Note: The Cray Fortran Compiler allows equivalencing of character data with noncharacter data. The Fortran standard does not address this. It is recommended that you do not perform equivalencing in this manner, however, because alignment and padding differs across platforms, thus rendering your code less portable.
If an equivalence object is of intrinsic type other than default integer, default real, double-precision real, default complex, or default logical, all objects in the set must be of the same type with the same kind type parameter value.
The use of an array name unqualified by a subscript list in an equivalence set specifies the first element of the array; that is, A means the first element of A.
An EQUIVALENCE statement must not specify that the same storage unit is to occur more than once in a storage sequence. For example, the following is illegal because it would indicate that storage for X(2) and X(3) is shared:
EQUIVALENCE (A, X(2)), (A, X(3)) |
An EQUIVALENCE statement must not specify the sharing of storage units between objects declared in different scoping units.
An EQUIVALENCE statement specifies that the storage sequences of the data objects in an equivalence set are storage associated. Any nonzero-sized sequences in the set have the same first storage unit, and any zero-sized sequences are storage associated with one another and with the first storage unit of any nonzero-sized sequences. This causes storage association of the objects in the group and may cause storage association of other data objects.
Example 1: The following code causes the alignment illustrated in Figure 5-2:
CHARACTER(LEN = 4) :: A, B CHARACTER(LEN = 3) :: C(2) EQUIVALENCE (A, C(1)), (B, C(2)) |
As a result, the fourth character of A, the first character of B, and the first character of C(2) all share the same character storage unit.
Example 2: Figure 5-3, illustrates alignment of the following two numeric arrays:
REAL, DIMENSION(6) :: X, Y EQUIVALENCE (X(5), Y(3)) |
The COMMON statement establishes blocks of storage called common blocks and specifies objects that are contained in the blocks. Two or more program units can 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. Common blocks can be named, in which case they are called named common blocks, or they can be unnamed, in which case they are called blank common.
Common blocks can contain mixtures of storage units and can contain unspecified storage units; however, if a common block contains a mixture of storage units, every declaration of the common block in the program must contain the same sequence of storage units, thereby matching types, kind type parameters, and attributes (DIMENSION and POINTER). The format of the COMMON statement is defined as follows:
Table 5-29.
| common_stmt | is |
| |
| common_block_object | is |
|
A common_block_object must not be one of the following items:
A dummy argument.
An allocatable variable.
A variable of derived type that contains an allocatable component at any level.
An automatic object.
A function name, result name, or entry name.
A USE associated variable.
A HOST associated variable.
A sequence structure with default initialization.
A nonsequence structure.
An object with the BIND attribute.
A variable in a COMMON block that has the BIND attribute.
The appearance of two slashes with no common block name between them declares that the objects that follow are in blank common.
A common block name or an indication of blank common can 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.
A variable can appear in only one common block within a scoping unit.
If a variable appears with an explicit-shape specification list, it is an array, and each bound must be a constant specification expression.
Only a named common block can be saved. Individual variables in the common block cannot be saved.
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 order of their appearance in the common block list. The storage sequence may be extended (on the end) to include the storage units of any variable equivalenced to a variable in the common block. Data objects storage associated with a variable in a common block are considered to be in that common block. The size of a common block is the size of its storage sequence including any extensions of the sequence resulting from equivalence association.
Within an executable program, the common block storage sequences of all nonzero-sized common blocks with the same name have the same first storage unit. Zero-sized common blocks are permitted. All zero-sized common blocks with the same name are storage associated with one another. The same is true for all blank common blocks except that because they can be of different sizes, 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. In this way, many subprograms can use the same storage. They can specify common blocks to communicate global values or to reuse and conserve storage. USE association or HOST association can cause these associated objects to be accessible in the same scoping unit.
A nonpointer object of type default integer, default real, double-precision real, default complex, default logical, or numeric sequence type must become associated with only nonpointer objects of these types.
A nonpointer object of type character or character sequence must become associated with only nonpointer objects of these types.
If an object of numeric sequence or character sequence type appears in a common block, it is as if the individual components were enumerated in order directly in the common block object list.
A nonpointer object of sequence type other than numeric or character sequence type must become associated only with nonpointer objects of the same type.
A nonpointer object of intrinsic type other than default integer, default real, double precision real, default complex, default logical, or character must become associated only with nonpointer objects of the same type with the same kind type parameter value.
A pointer must become associated only with pointers of the same type, type parameters, and rank.
Note: An object with the TARGET attribute can become storage associated only with another object that has the TARGET attribute and the same type and type parameters.
The Cray Fortran Compiler treats named common blocks and blank common blocks identically, as follows:
Variables in blank common and variables in named common blocks can be initialized.
Named common blocks and blank common are always saved.
Named common blocks of the same name and blank common can be of different sizes in different scoping units.
Note: The Fortran standard lists the following differences between blank common and named common blocks:
Variables in blank common must not be initially defined in type declaration statements or DATA statements.
A named common block is not saved unless it is named in a SAVE or STATIC statement.
Named common blocks of the same name must be of the same size in all scoping units.
Consider the code in the following example:
SUBROUTINE FIRST
REAL B(2)
COMPLEX C
LOGICAL FLAG
TYPE COORDINATES
SEQUENCE
REAL X, Y
LOGICAL Z_O ! ZERO ORIGIN?
END TYPE COORDINATES
TYPE(COORDINATES) P
COMMON /REUSE/ B, C, FLAG, P
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
. . .
END SUBROUTINE
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(100000) :: ONE, ANOTHER
INTEGER(SHORT) :: M(2000)
COMMON /MIXED/ V, ONE, ANOTHER, M ! ILLEGAL
. . .
END SUBROUTINE |
Common block REUSE has a storage sequence of 8 numeric storage units. It is used to conserve storage. The storage referenced in subroutine FIRST is associated with the storage referenced in subroutine SECOND, as follows:
The Fortran standard does not guarantee that the storage is actually retained and reused because, in the absence of a SAVE or STATIC attribute for REUSE, some compilers can release the storage when either of the subroutines completes execution. The Cray Fortran Compiler treats named common blocks as entities that are contained in a SAVE or STATIC statement.
Common block SHARE contains both numeric and character storage units and is 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 W in FIRST has two dimensions; the array pointer V in SECOND has only one. With common blocks, it is the sequence of storage units that must match, not the names of variables.
An EQUIVALENCE statement must not cause two different common blocks to become associated and must not cause a common block to be extended by adding storage units preceding the first storage unit of the common block. For example, the following code is legal and results in the alignment illustrated in Figure 5-5:
COMMON A(5) REAL B(5) EQUIVALENCE (A(2), B(1)) |
On the other hand, the following code is not legal because it would place B(1) ahead of A(1), as is illustrated in Figure 5-6:
EQUIVALENCE (A(1), B(2)) |
COMMON and EQUIVALENCE statements can appear in a module. The name of a public data object from a module must not appear in a COMMON or EQUIVALENCE statement in any scoping unit that has access to the data object.
EQUIVALENCE association must not cause a derived-type or type alias object with default initialization to be associated with an object in a common block.
| Prev Section | Table of Contents | Title Page | Index | Next Section |
| NAMELIST Statement | Up one level | Using Data |