4.3 COMMON Statement

A COMMON statement defines one or more contiguous areas, or blocks, of storage shared among separate subprograms. COMMON statements also define the order in which variables, arrays, and records are stored in each common block.

A symbolic name identifies each block. However, you can omit a symbolic name for one block in a program unit. The block without a name is known as the blank common block.

The COMMON statement takes the following form:

COMMON [/[cb]/] nlist[[,] /[cb] /nlist] . . .
cb
Is a symbolic name, called a common block name; cb can be blank. If the first cb is blank, you can omit the first pair of slashes.
nlist
Is a list of variable names, array names, array declarators, and record names separated by commas.

Rules and Behavior

Any common block name, blank or otherwise, 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.

You can use array declarators in the COMMON statement to define arrays.

A common block can have the same name as a variable, array, record, structure, or field. However, in a program with one or more program units, a common block cannot have the same name as a function, subroutine, or entry name in the executable program.

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 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, consider a program unit containing the following statement:

COMMON CENTS

Consider another program unit containing the following statements:

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.


Note
On Tru64 UNIX systems, when multiple object modules declare the same named common block, all modules must declare the common block to be the same size, or the module that gets loaded first must declare the common block to be its maximum defined length. If the common block is initialized by a DATA statement, the module containing the initialization must declare the common block to be its maximum defined length. This limitation does not apply if you compile all source modules together using full optimization, which allows interprocedural analysis to handle common block size differences. Loading of modules occurs in the order in which they are specified on the compiler or loader command line.

Example

In the following example, the COMMON statement in the main program puts HEAT and X in the blank common block, and KILO and Q in a named common block, BLK1.

The COMMON statement in the subroutine makes ALFA and BET share the same storage location as HEAT and X in the blank common block. It makes LIMA and R share the same storage location as KILO and Q in BLK1.
Main Program  Subprogram 
COMMON HEAT,X /BLK1/KILO,Q   SUBROUTINE FIGURE  
. . .  COMMON /BLK1/LIMA,R / /ALFA,BET  
CALL FIGURE   . . . 
. . .  RETURN  
  END  


Previous Page Next Page Table of Contents