A COMMON statement defines one or more contiguous areas, or blocks, of physical storage (called common blocks) that can be accessed by any of the scoping units in an executable program. COMMON statements also define the order in which variables and arrays are stored in each common block, which can prevent misaligned data items.
Common blocks can be named or unnamed (a blank common).
The COMMON statement takes the following form:
The variable must not be a dummy argument, allocatable array, automatic object, function, function result, or entry to a procedure. It must not have the PARAMETER attribute. If an object of derived type is specified, it must be a sequence type.
A common block is a global entity, and must not have the same name as any other global entity in the program, such as a subroutine or function.
Any common block name (or blank common) can appear more than once in one or more COMMON statements in a program unit. The list following each successive appearance of the same common block name is treated as a continuation of the list for the block associated with that name.
A variable can appear in only one common block within a scoping unit.
If an array is specified, it can be followed by an explicit-shape array specification. The array must not have the POINTER attribute and each bound in the specification must be a constant specification expression.
A pointer can only be associated with pointers of the same type and kind parameters, and rank.
An object with the TARGET attribute can only be associated with another object with the TARGET attribute and the same type and kind parameters.
A nonpointer can only be associated with another nonpointer, but association depends on their types, as follows:
Type of Variable | Type of Associated Variable |
---|---|
Intrinsic numeric 1 or numeric sequence 2 | Can be of any of these types |
Default character or character sequence 2 | Can be of either of these types |
Any other intrinsic type | Must have the same type and kind parameters |
Any other sequence type | Must have the same type |
1 Default integer, default real, double precision real,
default complex, double complex, or
default logical.
2 If an object of numeric sequence or character sequence type appears in a common block, it is as if the individual components were enumerated directly in the common list. |
So, variables can be associated if they are of different numeric type. For example, the following is valid:
INTEGER A(20)
REAL Y(20)
COMMON /QUANTA/ A, Y
When common blocks from different program units have the same name, they share the same storage area when the units are combined into an executable program.
Entities are assigned storage in common blocks on a one-for-one basis. So, the data type of entities assigned by a COMMON statement in one program unit should agree with the data type of entities placed in a common block by another program unit. For example:
Program Unit A | Program Unit B |
---|---|
COMMON CENTS |
INTEGER(2) MONEY |
. . . | COMMON MONEY |
. . . |
When these program units are combined into an executable program, incorrect results can occur if the 2-byte integer variable MONEY is made to correspond to the lower-addressed two bytes of the real variable CENTS.
Named common blocks must be declared to have the same size in each program unit. Blank common can have different lengths in different program units.
In the following example, the COMMON statement in the main program puts HEAT and X in blank common, and KILO and Q in a named common block, BLK1:
Main Program | Subprogram |
---|---|
COMMON HEAT,X /BLK1/KILO,Q
| SUBROUTINE FIGURE |
. . . | COMMON /BLK1/LIMA,R
/ /ALFA,BET |
. . . | |
CALL FIGURE
| |
. . . | RETURN |
END |
The COMMON statement in the subroutine makes ALFA and BET share the same storage location as HEAT and X in blank common. It makes LIMA and R share the same storage location as KILO and Q in BLK1.
The following example shows how a COMMON statement can be used to declare arrays:
COMMON / MIXED / SPOTTED(100), STRIPED(50,50)
For More Information: