Extending a Common Block with an EQUIVALENCE Statement

Last reviewed: December 11, 1995
Article ID: Q67229
The information in this article applies to:
  • Microsoft FORTRAN for MS-DOS, versions 4.0, 4.01, 4.1, 5.0, and 5.1
  • Microsoft FORTRAN for OS/2, versions 4.1, 5.0, and 5.1
  • Microsoft FORTRAN PowerStation for MS-DOS, versions 1.0 and 1.0a
  • Microsoft FORTRAN PowerStation 32 for Windows NT, version 1.0 and 4.0

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

  • 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: kbprg kbcode
    KBSubcategory: FORTLngIss


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: December 11, 1995
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.