Aligning Data Structure Subfields

Alignment of subfields may be necessary. In some cases it is done automatically; in others, it must be done manually.

For example, when defining subfields of type basing pointer or procedure pointer using the length notation, the compiler will automatically perform padding if necessary to ensure that the subfield is aligned properly.

When defining float, integer or unsigned subfields, alignment may be desired to improve run-time performance. If the subfields are defined using length notation, you can automatically align float, integer or unsigned subfields by specifying the keyword ALIGN on the data structure definition. However, note the following exceptions:
  • The ALIGN keyword is not allowed for a file information data structure or a program status data structure.
  • Subfields defined using the keyword OVERLAY are not aligned automatically, even if the keyword ALIGN is specified for the data structure. In this case, you must align the subfields manually.
Automatic alignment will align the fields on the following boundaries.
  • 2 bytes for 5-digit integer or unsigned subfields
  • 4 bytes for 10-digit integer or unsigned subfields or 4-byte float subfields
  • 8 bytes for 20-digit integer or unsigned subfields
  • 8 bytes for 8-byte float subfields
  • 16 bytes for pointer subfields

If you are aligning fields manually, make sure that they are aligned on the same boundaries. A start-position is on an n-byte boundary if ((position - 1) mod n) = 0. (The value of "x mod y" is the remainder after dividing x by y in integer arithmetic. It is the same as the MVR value after X DIV Y.)

Figure 1 shows a sequence of bytes and identifies the different boundaries used for alignment.

Figure 1. Boundaries for Data Alignment
This figure illustrates the boundaries for data alignment
Note the following about the above byte sequence:
  • Position 1 is on a 16-byte boundary, since ((1-1) mod 16) = 0.
  • Position 13 is on a 4-byte boundary, since ((13-1) mod 4) = 0.
  • Position 7 is not on a 4-byte boundary, since ((7-1) mod 4) = 2.