[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP Fortran for OpenVMS
Language Reference Manual


Previous Contents Index

5.4 COMMON Statement

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:


  • COMMON [/[cname]/] var-list [[,] /[cname]/ var-list]...

cname

Is the name of the common block. The name can be omitted for blank common (//).

var-list

Is a list of variable names, separated by commas.

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.

Rules and Behavior

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

1Default integer, default real, double precision real, default complex, double complex, or default logical.
2If 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.

A variable or COMMON block must be declared VOLATILE if it can be read or written in a way that is not visible to the compiler.

Examples

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:

  • On specification expressions, see Section 4.1.7.2.
  • On storage association, see Section 15.5.3.
  • On derived types, see Section 3.3.
  • On the EQUIVALENCE statement, see Section 5.7.
  • On the interaction between COMMON and EQUIVALENCE statements, see Section 5.7.3.
  • On alignment of data items in common blocks, see the HP Fortran for OpenVMS User Manual.
  • On the VOLATILE attribute and statement, see Section 5.19.

5.5 DATA Statement

The DATA statement assigns initial values to variables before program execution. It takes the following form:


  • DATA var-list /c-list/[[,] var-list /c-list/]...

var-list

Is a list of variables or implied-do lists, separated by commas.

Subscript expressions and expressions in substring references must be initialization expressions.

An implied-do list in a DATA statement takes the following form:


  • (do-list, var = expr1, expr2 [,expr3])

do-list

Is a list of one or more array elements, substrings, scalar structure components, or implied-do lists, separated by commas. Any array elements or scalar structure components must not have a constant parent.

var

Is the name of a scalar integer variable (the implied-do variable).

expr

Are scalar integer expressions. The expressions can contain variables of other implied-do lists that have this implied-do list within their ranges.

c-list

Is a list of constants (or names of constants), or for pointer objects, NULL(); constants must be separated by commas. If the constant is a structure constructor, each component must be an initialization expression. If the constant is in binary, octal, or hexadecimal form, the corresponding object must be of type integer.

A constant can be specified in the form r*constant, where r is a repeat specification. It is a nonnegative scalar integer constant (with no kind parameter). If it is a named constant, it must have been declared previously in the scoping unit or made accessible through use or host association. If r is omitted, it is assumed to be 1.

Rules and Behavior

A variable can be initialized only once in an executable program. A variable that appears in a DATA statement and is typed implicitly can appear in a subsequent type declaration only if that declaration confirms the implicit typing.

The number of constants in c-list must equal the number of variables in var-list. The constants are assigned to the variables in the order in which they appear (from left to right).

The following objects cannot be initialized in a DATA statement:

  • Dummy argument
  • Function
  • Function result
  • Automatic object
  • Allocatable array
  • Variable that is accessible by use or host association
  • Variable in a named common block (unless the DATA statement is in a block data program unit)
  • Variable in blank common

Except for variables in named common blocks, a named variable has the SAVE attribute if any part of it is initialized in a DATA statement. You can confirm this property by specifying the variable in a SAVE statement or a type declaration statement containing the SAVE attribute.

When an unsubscripted array name appears in a DATA statement, values are assigned to every element of that array in the order of subscript progression. The associated constant list must contain enough values to fill the array.

Array element values can be initialized in three ways: by name, by element, or by an implied-do list (interpreted in the same way as a DO construct).

The following conversion rules and restrictions apply to variable and constant list items:

  • If the constant and the variable are both of numeric type, the following conversion occurs:
    • The constant value is converted to the data type of the variable being initialized, if necessary.
    • When a binary, octal, or hexadecimal constant is assigned to a variable or array element, the number of digits that can be assigned depends on the data type of the data item. If the constant contains fewer digits than the capacity of the variable or array element, the constant is extended on the left with zeros. If the constant contains more digits than can be stored, the constant is truncated on the left.
  • If the constant and the variable are both of character type, the following conversion occurs:
    • If the length of the constant is less than the length of the variable, the rightmost character positions of the variable are initialized with blank characters.
    • If the length of the constant is greater than the length of the variable, the character constant is truncated on the right.
  • If the constant is of numeric type and the variable is of character type, the following restrictions apply:
    • The character variable must have a length of one character.
    • The constant must be an integer, binary, octal, or hexadecimal constant, and must have a value in the range 0 through 255.

    When the constant and variable conform to these restrictions, the variable is initialized with the character that has the ASCII code specified by the constant. (This lets you initialize a character object to any 8-bit ASCII code.)
  • If the constant is a Hollerith or character constant, and the variable is a numeric variable or numeric array element, the number of characters that can be assigned depends on the data type of the data item.
    If the Hollerith or character constant contains fewer characters than the capacity of the variable or array element, the constant is extended on the right with blank characters. If the constant contains more characters than can be stored, the constant is truncated on the right.

Examples

The following example shows the three ways that DATA statements can initialize array element values:


DIMENSION A(10,10)

DATA A/100*1.0/    ! initialization by name

DATA A(1,1), A(10,1), A(3,3) /2*2.5, 2.0/ ! initialization by element

DATA ((A(I,J), I=1,5,2), J=1,5) /15*1.0/  ! initialization by implied-do list

The following example shows DATA statements containing structure components:


TYPE EMPLOYEE
  INTEGER ID
  CHARACTER(LEN=40) NAME
END TYPE EMPLOYEE
TYPE(EMPLOYEE) MAN_NAME, CON_NAME
DATA MAN_NAME / EMPLOYEE(417, 'Henry Adams') /
DATA CON_NAME%ID, CON_NAME%NAME /891, "David James"/

In the following example, the first DATA statement assigns zero to all 10 elements of array A, and four asterisks followed by two blanks to the character variable STARS:


INTEGER A(10), B(10)
CHARACTER BELL, TAB, LF, FF, STARS*6
DATA A,STARS /10*0,'****'/
DATA BELL,TAB,LF,FF /7,9,10,12/
DATA (B(I), I=1,10,2) /5*1/

In this case, the second DATA statement assigns ASCII control character codes to the character variables BELL, TAB, LF, and FF. The last DATA statement uses an implied-do list to assign the value 1 to the odd-numbered elements in the array B.

As a Fortran 95 feature, a pointer can be initialized as disassociated by using a DATA statement. For example:


INTEGER, POINTER :: P
DATA P/NULL()/
END

For More Information:

5.6 DIMENSION Attribute and Statement

The DIMENSION attribute specifies that an object is an array, and defines the shape of the array.

The DIMENSION attribute can be specified in a type declaration statement or a DIMENSION statement, and takes one of the following forms:

Type Declaration Statement:


  • type, [att-ls,] DIMENSION (a-spec) [,att-ls] :: a[(a-spec)] [,a[(a-spec)]]...

Statement:


  • DIMENSION [::] a(a-spec) [,a(a-spec)]...

type

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

a-spec

Is an array specification.

In a type declaration statement, any array specification following an array overrides any array specification following DIMENSION.

a

Is the name of the array being declared.

Rules and Behavior

The DIMENSION attribute allocates a number of storage elements to each array named, one storage element to each array element in each dimension. The size of each storage element is determined by the data type of the array.

The total number of storage elements assigned to an array is equal to the number produced by multiplying together the number of elements in each dimension in the array specification. For example, the following statement defines ARRAY as having 16 real elements of 4 bytes each and defines MATRIX as having 125 integer elements of 4 bytes each:


DIMENSION ARRAY(4,4), MATRIX(5,5,5)

An array can also be declared in the following statements: ALLOCATABLE, POINTER, TARGET, and COMMON.

Examples

The following examples show type declaration statements specifying the DIMENSION attribute:


REAL, DIMENSION(10, 10) :: A, B, C(10, 15)  ! Specification following C
                                            ! overrides the one following
                                            ! DIMENSION
REAL, ALLOCATABLE, DIMENSION(:) :: E

The following are examples of the DIMENSION statement:


DIMENSION BOTTOM(12,24,10)
DIMENSION X(5,5,5), Y(4,85), Z(100)
DIMENSION MARK(4,4,4,4)

SUBROUTINE APROC(A1,A2,N1,N2,N3)
DIMENSION A1(N1:N2), A2(N3:*)

CHARACTER(LEN = 20) D
DIMENSION A(15), B(15, 40), C(-5:8, 7), D(15)

For More Information:

5.7 EQUIVALENCE Statement

The EQUIVALENCE statement specifies that a storage area is shared by two or more objects in a program unit. This causes total or partial storage association of the objects that share the storage area.

The EQUIVALENCE statement takes the following form:


  • EQUIVALENCE (equiv-list) [,(equiv-list)]...

equiv-list

Is a list of two or more variables, array elements, or substrings, separated by commas (also called an equivalence set). If an object of derived type is specified, it must be a sequence type. Objects cannot have the TARGET attribute.

Each expression in a subscript or a substring reference must be an integer initialization expression. A substring must not have a length of zero.

Rules and Behavior

The following objects cannot be specified in EQUIVALENCE statements:

  • Dummy argument
  • Allocatable array
  • Pointer
  • Object of nonsequence derived type
  • Object of sequence derived type containing a pointer in the structure
  • Function, entry, or result name
  • Named constant
  • Structure component
  • Subobject of any of the above objects

The EQUIVALENCE statement causes all of the entities in one parenthesized list to be allocated storage beginning at the same storage location.

Association of objects depends on their types, as follows:

Type of Object Type of Associated Object
Intrinsic numeric 1 or numeric sequence Can be of any of these types
Default character or character sequence Can be of either of these types 2
Any other intrinsic type Must have the same type and kind parameters
Any other sequence type Must have the same type

1Default integer, default real, double precision real, default complex, double complex, or default logical.
2The lengths do not have to be equal.

So, objects can be associated if they are of different numeric type. For example, the following is valid:


INTEGER A(20)
REAL Y(20)
EQUIVALENCE(A, Y)

Objects of default character do not need to have the same length. The following example associates character variable D with the last 4 (of the 6) characters of character array F:


CHARACTER(LEN=4) D
CHARACTER(LEN=3) F(2)
EQUIVALENCE(D, F(1)(3:))

Entities having different data types can be associated because multiple components of one data type can share storage with a single component of a higher-ranked data type. For example, if you make an integer variable equivalent to a complex variable, the integer variable shares storage with the real part of the complex variable.

The same storage unit cannot occur more than once in a storage sequence, and consecutive storage units cannot be specified in a way that would make them nonconsecutive.

Examples

The following EQUIVALENCE statement is invalid because it specifies the same storage unit for X(1) and X(2):


REAL, DIMENSION(2), :: X
REAL :: Y
EQUIVALENCE(X(1), Y), (X(2), Y)

The following EQUIVALENCE statement is invalid because because A(1) and A(2) will not be consecutive:


REAL A(2)
DOUBLE PRECISION D(2)
EQUIVALENCE(A(1), D(1)), (A(2), D(2))

In the following example, the EQUIVALENCE statement causes the four elements of the integer array IARR to share the same storage as that of the double-precision variable DVAR.


DOUBLE PRECISION DVAR
INTEGER(KIND=2) IARR(4)
EQUIVALENCE(DVAR, IARR(1))

In the following example, the EQUIVALENCE statement causes the first character of the character variables KEY and STAR to share the same storage location. The character variable STAR is equivalent to the substring KEY(1:10).


CHARACTER KEY*16, STAR*10
EQUIVALENCE(KEY, STAR)

For More Information:


Previous Next Contents Index