Extending a Common Block with an EQUIVALENCE Statement

ID: Q67229

4.00 4.01 4.10 5.00 5.10 1.00 1.00a | 4.10 5.00 5.10 | 1.00 4.00

MS-DOS                              | OS/2           | WINDOWS NT

The information in this article applies to:

SUMMARY

An EQUIVALENCE statement can be used to extend a common block size by EQUIVALENCing the last element of a common block with the first element of an array that is not in a common block. This is demonstrated in the code below.

This article expands upon the information in the EQUIVALENCE statement section of the "Microsoft FORTRAN Reference" manual, which discusses extending common blocks found.

MORE INFORMATION

The following code fragment shows a common block being extended from 4 bytes to 800 bytes in length.

C ***** FORTRAN program fragment *****

      REAL*4 VAR, A(200)
      COMMON /TEST/ VAR
      EQUIVALENCE (VAR, A(1))

In this fragment, the common block TEST is initially 4 bytes in length. The equivalence statement specifies that VAR and A(1) will share the same location in memory. Because all of A's elements are contiguous in memory, the common block TEST is extended to be 200 x 4 or 800 bytes in length. This is shown graphically below:

   |01|02|03|04|05|06|07|08|09|0A|0B|0C|0D|0E|0F|10|
   |------------------------------------------------
   |....VAR....|
   |------------------------------------------------
   |....A(1)...|....A(2)...|....A(3)...|....A(4)...|
   |------------------------------------------------
   |<-- Beginning address of common block TEST

The guidelines listed below must be followed when using EQUIVALENCE to extend a common block's length:

1. A common block may only be extended by adding elements to the end

   of the common block. An EQUIVALENCE operation cannot add elements
   that precede the common block as in the following example:

C ***** FORTRAN program fragment *****

      REAL*4 VAR, A(200)
      COMMON /TEST/ VAR
      EQUIVALENCE (VAR, A(2))

   Here VAR and A(2) will share the same memory location. Because A(1)
   precedes A(2) in memory, it will also precede VAR, which is the
   beginning of the common block. This is called extending a common
   block forward and is shown graphically below:

      |??|??|??|??|01|02|03|04|05|06|07|08|09|0A|0B|0C|0D|0E|0F|10
      ------------|------------------------------------------------
      ............|....VAR....|
      ------------|------------------------------------------------
      |....A(1)...|....A(2)...|....A(3)...|....A(4)...|....A(5)...
      ------------|------------------------------------------------
      ............|<-- Beginning address of common block TEST

   The following error is generated when the code listed above is
   compiled:

      F2320: A : EQUIVALENCE : extends common block TEST forward

2. The ANSI FORTRAN 77 Standard states that a NAMED common block must
   be of the same size in all program units of an executable program
   in which it appears. For this reason, when a NAMED common block is
   extended, every subsequent subprogram accessing the common block
   must declare it according to its new size. For example, in the
   code below, a NAMED common block is again declared with one REAL*4
   element and then EQUIVALENCEd with a 200 element REAL*4 array. In
   the subroutine, the common block is then declared with its new size
   of 200 elements:

C ***** FORTRAN program fragment *****

      PROGRAM main
      REAL*4 VAR, A(200)
      COMMON /TEST/ VAR
      EQUIVALENCE (VAR, A(1))
      CALL sub()
      .
      .
      END

      SUBROUTINE sub ()
      REAL*4 B
      COMMON /TEST/ B(200)
      .
      .
      RETURN
      END

   However, FORTRAN 5.0 will allow a NAMED common block to have
   different sizes in subprogram units. In this situation, the compiler
   generates the following warning:

      F4329: TEST : COMMON : size changed

   If the /4Ys compile option or $STRICT metacommand is used with
   FORTRAN 5.0, or if the program is compiled with FORTRAN 4.x, the
   error

      F2323: TEST : COMMON : size changed

   is generated when a NAMED common block's size is changed in a
   subprogram.

Under Fortran PowerStation 4.0 using the /4Ys compile option or the $STRICT metacommnad, a change in a subprogram common block's size generates the following error message:

   error FOR3750: byte count on numeric data type detected
   between * and 4

Additional reference words: kbinf 1.00 4.00 5.00 5.10 KBCategory: KBSubcategory: FORTLngIss
Keywords          : kbcode kbFortranPS kbLangFortran 
Version           : 4.00 4.01 4.10 5.00 5.10 1.00 1.
Platform          : MS-DOS NT OS/2 WINDOWS

Last Reviewed: May 23, 1998