Arranging Data Items in Common Blocks

The order of data items in a COMMON statement determine the order in which the data items are stored. Consider the following declaration of a common block named X:

  LOGICAL (KIND=2) FLAG
  INTEGER          IARRY_I(3)
  CHARACTER(LEN=5) NAME_CH
  COMMON /X/ FLAG, IARRY_I(3), NAME_CH

As shown in the following figure, if you omit the alignment compiler options, the common block will contain unaligned data items beginning at the first array element of IARRY_I.

Common Block with Unaligned Data

As shown in the following figure, if you compile the program units that use the common block with the /align:commons options, data items will be naturally aligned.

Common Block with Naturally Aligned Data

Because the common block X contains data items whose size is 32 bits or smaller, specify /align:commons. If the common block contains data items whose size might be larger than 32 bits (such as REAL (KIND=8) data), use /align:dcommons.

If you can easily modify the source files that use the common block data, define the numeric variables in the COMMON statement in descending order of size and place the character variable last to provide more portability and ensure natural alignment without padding or the DF command options /align:commons or /align:dcommons:

  LOGICAL (KIND=2) FLAG
  INTEGER          IARRY_I(3)
  CHARACTER(LEN=5) NAME_CH
  COMMON /X/ IARRY_I(3), FLAG, NAME_CH

As shown in the following figure, if you arrange the order of variables from largest to smallest size and place character data last, the data items will be naturally aligned.

Common Block with Naturally Aligned Reordered Data

When modifying or creating all source files that use common block data, consider placing the common block data declarations in a module so the declarations are consistent. If the common block is not needed for compatibility (such as file storage or Compaq Fortran 77 use), you can place the data declarations in a module without using a common block.