Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 10 HP Fortran statements

COMMON

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

Specifies common blocks.

Syntax

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

is the name of a labeled common block.

object-list

is a comma-separated list of scalar variables, arrays, records, and derived-type objects. If an array is specified, it may be followed by an explicit-shape specification expression.

Description

The COMMON statement defines one or more storage areas to be shared by different program units. It also identifies the objects—that is, variables, arrays, records, and derived-type objects—to be stored in those areas. Objects in common that are shared by different program units are made accessible by storage association.

Each object following a common-block name is declared to be in that common block. If /common-block-name/ is omitted, all objects in the corresponding object-list are specified to be in blank common. It is also possible to declare variables in blank common by specifying two slashes without common-block-name. Consider the following examples:

!Declare variables a, b, c in blank common.
COMMON a, b, c

! Declare pay and time in blank common,
! and red in the named common block color.
COMMON pay, time, /color/red

! Variables a1 and a2 are in common block a; array x and variable
! are in blank common; and variable d is in common block c
COMMON/a/a1,a2,//x(10),y,/c/d

Any common block name or blank common specification can appear more than once in one or more COMMON statements within the same program unit. The variable list following each successive appearance of the same common block name is treated as a continuation of the list for that common block name. For example, the following COMMON statements:

COMMON a,b,c /x/y,x,d //w,r
COMMON /cap/hat,visor, //tax, /x/o,t

are equivalent to:

COMMON a,b,c,w,r,tax
COMMON /x/y,x,d,o,t
COMMON /cap/hat,visor

Unlike named common blocks, blank common can differ in size in different scoping units. However, blank common cannot be initialized.

As an extension, HP Fortran saves all common blocks in static memory.

The following restrictions apply to the use of common blocks:

  • All common block names must be distinct from subprogram names.

  • The size of a named common block must be the same in all program units where it is declared. Note, however, that the size of blank common can differ.

  • The following data items must not appear in a COMMON statement:

    • Dummy arguments in a subprogram

    • Functions, subroutines, or intrinsic functions

    • Pointees declared by Cray-style pointers

    • Variables accessible by use association

    • Automatic entities, including automatic character strings

    • Allocatable arrays

  • Derived-type objects may appear in common if they have been defined with the SEQUENCE attribute.

  • A variable can only appear in one COMMON statement within a program unit.

  • Zero-sized common blocks are allowed. Zero-sized common blocks with the same name are storage associated.

  • Array bounds in a COMMON statement must be constant specification expressions.

  • A pointer may appear in common if it has the same type, type parameter, and rank in every instance of that common block.

Initializing common blocks

As an extension to the Standard, HP Fortran allows common blocks to be initialized outside of a block data program unit; for example, in a subroutine. However, note that all data initialization for a given common block must occur in the same compilation unit.

HP Fortran also allows blank—or unnamed—common to be initialized.

Common block size

The size of a common block is determined by the number and type of the variables it contains. In the following example, the common block my_block takes 20 bytes of storage: b uses 8 (2 bytes per element) and arr uses 12 (4 bytes per element):

INTEGER(2) b(4)
INTEGER(4) arr(3)
COMMON /cb/b, arr

Data space within the common area for arrays b and arr shown in this example is allocated as follows:

Table 10-5 Title not available (COMMON)

Bytes

Common block variables

0, 1, 2, 3

b(1), b(2)

4, 5, 6, 7

b(3), b(4)

8, 9, 10, 11

arr(1)

12, 13, 14, 15

arr(2)

16, 17, 18, 19

arr(3)

 

Allocation common block storage

Common block storage is allocated at link time. It is not local to any one program unit.

Each program unit that uses the common block must include a COMMON statement that contains the block name, if a name was specified. Variables assigned to the common block by the program unit need not correspond by name, type, or number of elements with those of any other program unit. The only consideration is the size of the common blocks referenced by the different program units. Correspondence between objects in different instances of the same common block is established by storage association.

Note the following for HP Fortran: when types with different alignment restrictions are mixed in a common block, the compiler may insert padding bytes as necessary.

Examples

The following example illustrates how the same common block can be declared in different program units with different variables but the same size:

! common declaration for program unit 1
INTEGER i, j, k
COMMON /my_block/ i, j, k

! common declaration for program unit 2
INTEGER n(3)
COMMON /my_block/ n(3)

The variables i, j, and k in program unit 1 share the same storage with the array n in program unit 2: i in program unit 1 matches up with n(1) in program unit 2, j with n(2), and k with n(3).

Related statements

EQUIVALENCE

Related concepts

For information about data alignment, see Table 3-1 “Intrinsic data types” and “Alignment of derived-type objects”.

Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.