HP 3000 Manuals

The EQUIVALENCE Statement [ HP FORTRAN 77/iX Programmer's Guide ] MPE/iX 5.0 Documentation


HP FORTRAN 77/iX Programmer's Guide

The EQUIVALENCE Statement 

Storage space is allocated in memory consecutively; every variable is
independent.  Except for common variables, the declaration order of
variables in the source code does not determine the order in which the
variables are allocated in memory.  However, this pattern of allocation
can be changed with the EQUIVALENCE statement, which allows overlapping
of the same storage space with more than one variable.  Care must be
taken when data types of different sizes share the same storage space.
This section describes how to use the EQUIVALENCE statement in situations
that require special attention.

Equivalence of Array Elements 

Array elements can share the same storage space with elements of a
different array or with simple variables.  For example, the statements:

     REAL*4 a(3), c(5)
     EQUIVALENCE (a(2), c(4))

specify that array element a(2) shares the same storage space as array
element c(4).  This implies that:

   *   a(1) shares storage space with c(3)

   *   a(3) shares storage space with c(5)

   *   No equivalence occurs outside the bounds of the arrays

The storage space for the two arrays is shown in the following table:

----------------------------------------------------
|              |                    |              |
|   Array a    |   Storage Space    |   Array c    |
|              |    Byte Number     |              |
|              |                    |              |
----------------------------------------------------
|              |                    |              |
|              |         1-4        | c(1)         |
|              |                    |              |
----------------------------------------------------
|              |                    |              |
|              |         5-8        | c(2)         |
|              |                    |              |
----------------------------------------------------
|              |                    |              |
|     a(1)     |        9-12        | c(3)         |
|              |                    |              |
----------------------------------------------------
|              |                    |              |
|     a(2)     |   <-- 13-16 -->    | c(4)         |
|              |                    |              |
----------------------------------------------------
|              |                    |              |
|     a(3)     |       17-20        | c(5)         |
|              |                    |              |
----------------------------------------------------

By using the EQUIVALENCE statement, array elements can share the same
storage space.  If the arrays are not of the same type, they might not
line up element-by-element.  For example, the statements:

     REAL*4 a(2)
     INTEGER*2 ibar(4)
     EQUIVALENCE (a(1), ibar(1))

produce the following storage space allocation:

----------------------------------------------
|              |              |              |
|   Array a    |   Storage    |  Array ibar  |
|              |    Space     |              |
|              | Word Number  |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|     a(1)     |    1 - 4     | ibar(1)      |
|              |              | ibar(2)      |
|              |              |              |
----------------------------------------------
|              |              |              |
|     a(2)     |    5 - 8     | ibar(3)      |
|              |              | ibar(4)      |
|              |              |              |
----------------------------------------------

Placing an array name only in an EQUIVALENCE statement has the same
effect as using an array element name that specifies the first element of
the array.  That is, the statement:

     EQUIVALENCE (a,ibar)

produces the same results as:

     EQUIVALENCE(a(1), ibar(1))

When array elements share the same storage space with other array
elements or variables, the same storage space cannot be occupied by more
than one element of the same array.  For example, the statements:

     DIMENSION a(2)
     EQUIVALENCE (a(1),b), (a(2), b)

are illegal because they specify the same storage space for a(1) and
a(2).

An EQUIVALENCE statement must not specify that consecutive array elements
are noncontiguous.  For example, the statements:

     REAL a(2), r(3)
     EQUIVALENCE (a(1), r(1)), (a(2), r(3))

are illegal because the EQUIVALENCE statement specifies that a(1) and
a(2) are noncontiguous.

Equivalence and Multi-Dimensioned Arrays 

As an extension to the ANSI standard, you can indicate on an EQUIVALENCE
statement the element of a multi-dimensioned array by specifying its
position in the array.  For example, the statements:

     INTEGER*4 total (3,2)
     INTEGER*4 sum (6)
     EQUIVALENCE (sum, total(1))

produces the following storage space allocation:

-------------------------------------------------
|                       |                       |
|     Array Element     |      Equivalent       |
|                       |     Array Element     |
|                       |                       |
-------------------------------------------------
|                       |                       |
| total(1,1)            | sum(1)                |
|                       |                       |
-------------------------------------------------
|                       |                       |
| total(2,1)            | sum(2)                |
|                       |                       |
-------------------------------------------------
|                       |                       |
| total(3,1)            | sum(3)                |
|                       |                       |
-------------------------------------------------
|                       |                       |
| total(1,2)            | sum(4)                |
|                       |                       |
-------------------------------------------------
|                       |                       |
| total(2,2)            | sum(5)                |
|                       |                       |
-------------------------------------------------
|                       |                       |
| total(3,2)            | sum(6)                |
|                       |                       |
-------------------------------------------------

According to the ANSI standard, an element of a multi-dimensioned array
must be referenced by one subscript for each dimension.  However, using
the equivalence and multi-dimensioned array feature, you can specify one
subscript (in EQUIVALENCE statements only) to indicate a position in
memory independent of an array's declared number of dimensions.

Equivalence Between Arrays of Different Dimensions 

To determine equivalence between arrays with different dimensions,
FORTRAN contains an internal array successor function that views all
elements of an array in linear sequence.  Each array is stored as if it
were a one-dimensional array.  Array elements are stored in ascending
sequential column-major order.  The first index varies the fastest, then
the second, and so on.  For example, the array:

     i(-2:4)

stores the elements of array i in this order:

     i(-2)  i(-1)  i(0)  i(1)  i(2)  i(3)  i(4)

The array:

     t(2,3)

stores the elements in the following order:

     t(1,1)  t(2,1)  t(1,2)  t(2,2)  t(1,3)  t(2,3)

Similarly, the array:

     k(2,2,3)

stores the elements in the following order (reading left to right and top
to bottom by row):

     k(1,1,1)     k(2,1,1)     k(1,2,1)     k(2,2,1)
     k(1,1,2)     k(2,1,2)     k(1,2,2)     k(2,2,2)
     k(1,1,3)     k(2,1,3)     k(1,2,3)     k(2,2,3)

The number of bytes each element occupies depends on the type of the
array.  For example, the statements:

     REAL*4      a
     INTEGER*2   i
     DIMENSION   a(2,2), i(4)
     EQUIVALENCE (a(2,1), i(2))

produce the following storage space allocation:

----------------------------------------------
|              |              |              |
|   Array a    |   Storage    |   Array i    |
|              |    Space     |              |
|              | Byte Number  |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|    a(1,1)    |    1 - 2     | i(1)         |
|              |    3 - 4     |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|    a(2,1)    |    5 - 6     | i(2)         |
|              |    7 - 8     | i(3)         |
|              |              |              |
----------------------------------------------
|              |              |              |
|    a(1,2)    |    9 - 10    | i(4)         |
|              |   11 - 12    |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|    a(2,2)    |   13 - 16    |              |
|              |              |              |
----------------------------------------------

Equivalence of Character Variables 

As an extension to the ANSI 77 Standard, character and noncharacter data
items can share the same storage space.  For example, the statements:

     INTEGER*4 i(5)
     CHARACTER*16 c
     EQUIVALENCE(i,c)

produce the following storage space allocation:

----------------------------------------------
|              |              |              |
|   Array i    |   Storage    |  Variable c  |
|              |    Space     |              |
|              | Byte Number  |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|  i(1)        |    1 - 4     | c(1:4)       |
|              |              |              |
----------------------------------------------
|              |              |              |
|  i(2)        |    5 - 8     | c(5:8)       |
|              |              |              |
----------------------------------------------
|              |              |              |
|  i(3)        |    9 - 12    | c(9:12)      |
|              |              |              |
----------------------------------------------
|              |              |              |
|  i(4)        |   13 - 16    | c(13:16)     |
|              |              |              |
----------------------------------------------
|              |              |              |
|  i(5)        |   17 - 20    |              |
|              |              |              |
----------------------------------------------

The lengths of the data items that share the same storage space do not
have to match.  An EQUIVALENCE statement specifies that the storage
sequence of the character data items whose names are specified in the
list have the same first character storage unit.  This causes the
association of the data items in the list and can cause association of
other data items.  Any adjacent characters in the associated data items
can also have the same character storage unit and therefore can also be
associated.  For example, the statements:

     CHARACTER*4 a, b
     CHARACTER*3 c(2)
     EQUIVALENCE (a,c(1)), (b,c(2))

cause association between a, b, and c in this way:
_________________________________________________
|                                               |
|     bytes | 01 | 02 | 03 | 04 | 05 | 06 | 07 ||
|                                               |
|           |<------- a ------->|               |
|                                               |
|                          |<------- b ------->||
|                                               |
|           |<--- c(1) --->|<--- c(2) --->|     |
_________________________________________________

            

Equivalence in Common Blocks 

Data elements can be put into a common block by specifying the elements
as equivalent to data elements mentioned in a COMMON statement.  If one
element of an array shares the same storage space with a data element in
a common block by using the EQUIVALENCE statement, the whole array is
placed in the common block.  Equivalence is maintained for the storage
unit preceding and following the data element in common.

When necessary, the common block is extended to fit an equivalenced array
into the common block.  However, no array can be put into a common block
with the EQUIVALENCE statement if storage elements have to be prefixed to
the common block to contain the entire array.

Equivalences cannot insert storage into the middle of the common block or
rearrange storage within the block.  Because the elements in a common
block are stored contiguously in the order they are listed in the COMMON
statement, two elements in common cannot be made to share the same
storage space with the EQUIVALENCE statement.  For example, in the
statements:

     INTEGER*4 i(6), j(6)
     COMMON i
     EQUIVALENCE (i(3), j(2))

the array i is in a common block and array element j is equivalent to
i(3).

The common block is extended to accommodate array j as follows:

----------------------------------------------
|              |              |              |
|   Array i    |   Storage    |  Variable j  |
|              |    Space     |              |
|              | Byte Number  |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|     i(1)     |   1 - 4      |              |
|              |              |              |
----------------------------------------------
|              |              |              |
|     i(2)     |   5 - 8      |     j(1)     |
|              |              |              |
----------------------------------------------
|              |              |              |
|     i(3)     |   9 - 12     |     j(2)     |
|              |              |              |
----------------------------------------------
|              |              |              |
|     i(4)     |  13 - 16     |     j(3)     |
|              |              |              |
----------------------------------------------
|              |              |              |
|     i(5)     |  17 - 20     |     j(4)     |
|              |              |              |
----------------------------------------------
|              |              |              |
|     i(6)     |  21 - 24     |     j(5)     |
|              |              |              |
----------------------------------------------
|              |              |              |
|              |  25 - 28     |     j(6)     |
|              |              |              |
----------------------------------------------

The equivalence set up by the statements:

     INTEGER*4 i(6), j(6)
     COMMON i
     EQUIVALENCE (i(1), j(2))

is not allowed.  To set array j into the common block, four extra bytes
must be inserted in front of the common block and element j(1) would be
stored in front of the common block.

Equivalence and Data Alignment 

Each data type has its own data alignment requirement that is system
dependent; see the HP FORTRAN 77/iX Reference for details.  If you force
any variable to start on a boundary other than the alignment required, a
compilation error occurs.  For example, if the data alignment of
character variables is on any byte boundary and the data alignment of
INTEGER*2 variables is on even byte (16-bit) boundaries, the following is
illegal:

     INTEGER*2 i, j(2)
     CHARACTER*6 c
     EQUIVALENCE (c, i)
     EQUIVALENCE (c(2:2), j)

In these statements, either i or j would have to start on an odd byte
boundary, which violates the alignment requirement.  However, if the
equivalence of c and i is removed, the statements:

     INTEGER*2 i, j(2)
     CHARACTER*6 c
     EQUIVALENCE (c(2:2), j)

produce the following storage space allocation:

----------------------------------------------------------
|              |              |                          |
|   Array j    |   Storage    |        Variable c        |
|              |    Space     |                          |
|              | Byte Number  |                          |
|              |              |                          |
----------------------------------------------------------
|              |              |                          |
|              |    0 - 1     | 1 unused byte, c(1:1)    |
|              |              |                          |
----------------------------------------------------------
|              |              |                          |
| j(1)         |    2 - 3     | c(2:3)                   |
|              |              |                          |
----------------------------------------------------------
|              |              |                          |
| j(2)         |    4 - 5     | c(4:5)                   |
|              |              |                          |
----------------------------------------------------------
|              |              |                          |
|              |    6 - 7     | c(6:6), 1 unused byte    |
|              |              |                          |
----------------------------------------------------------

Note that c starts on an odd byte boundary and j starts on an even byte
(16-bit word) boundary.



MPE/iX 5.0 Documentation