[an error occurred while processing this directive]

HP OpenVMS Systems Documentation

Content starts here

HP COBOL
Reference Manual


Previous Contents Index

5.2.2 COBOL Standard Alignment Rules

The COBOL Standard Alignment Rules specify how characters are positioned in an elementary data item. Positioning depends on the item's category:

  1. For a numeric receiving data item:
    • The data is aligned by decimal point. It is moved to the receiving character positions with zero fill or truncation, if necessary.
    • When an assumed decimal point is not explicitly specified, the data item is treated as if it had an assumed decimal point immediately after its rightmost character. It is then aligned by decimal point as described in the preceding list item.
  2. For a numeric edited receiving data item, the data is aligned by decimal point with zero fill or truncation, if necessary. Editing requirements can replace leading zeros with some other symbol.
  3. For receiving data items that are alphabetic, alphanumeric edited, or alphanumeric (without editing), the data is aligned at the leftmost character position in the data item, with space fill or truncation to the right, if necessary.

If the JUSTIFIED clause applies to the receiving item, the rules for the JUSTIFIED clause override rule 3. See the Section 5.3.28 clause for more information.

5.2.3 Additional Alignment Rules for Record Allocation

As stated in Section 5.2.2, the COBOL Standard Alignment Rules specify data positioning only within elementary data items. Hewlett-Packard defines additional alignment rules that affect the positioning of:

  • Records on the file media
  • Group items within a record
  • Elementary items within a group item

On Alpha and I64 systems, HP COBOL offers the option of allocating subordinate record items along performance-optimal boundaries through the use of the alignment compiler option or directives (or the SYNCHRONIZED clause). If you select one of these options, subordinate data items will be aligned automatically along optimal boundaries for their data type. The compiler may have to skip one or more bytes before assigning a location to the next data item. These skipped bytes, called fill bytes, are spaces between one data item and the next. Refer to the HP COBOL User Manual for information on using alignment compiler options and directives.

If you do not select one of these Alpha- and I64-only alignment options, the HP COBOL compiler will locate the data item at the next unassigned byte location.

The presence of fill bytes can make a record's structure different from what you might expect. In particular, if a record contains many items requiring alignment, its size can increase significantly. If, unaware of the fill bytes, you tried to move a group item containing fill bytes to a single data item, right-end truncation would occur. You would not have this problem, however, if you moved the record into another identically defined group item. The method the compiler uses to allocate storage ensures that identically described group items have the same structure, even when their subordinate items are aligned on their required boundaries. <>

Figure 5-3 shows alignment boundaries for a record. The boundary is the leftmost location of the 1-, 2-, 4-, or 8-byte area. All boundaries are relative to the beginning of the record as byte number 0.

Figure 5-3 Record Alignment Boundaries


The HP COBOL compiler allocates storage for data items within records according to the rules of the major-minor equivalence technique. The major-minor equivalence technique ensures that identically defined group items have the same structure, even when their subordinate items are aligned. Therefore, group moves always produce predictable results. This technique is based on the following two rules:

  • Location Equivalence---The leftmost location of a group item is the same as the leftmost location of its first subordinate item.
  • Boundary Equivalence---The HP COBOL compiler aligns a group item on a boundary that is as large as the largest boundary for any aligned data item within its scope.

Location Equivalence

Location equivalence forces a group (major) item to the same storage location as its first subordinate (minor) item. This forced positioning occurs regardless of the boundary alignment of either the group or subordinate item.

Refer to the HP COBOL User Manual chapter on aligning binary data for information on how location equivalence allocates storage.

The following example results in the major-minor location format:


 01  ITEM-A.
    03  ITEM-B.
        05  ITEM-C PIC 9(4) COMP SYNCHRONIZED.
    03  FILLER PIC X.
    03  ITEM-D.
        05  ITEM-E PIC 9(4) COMP SYNCHRONIZED.
    03  ITEM-F PIC X.

The following example (omitting SYNCHRONIZED) results in the left-right location format:


 01  ITEM-A.
    03  ITEM-B.
        05  ITEM-C PIC 9(4) COMP.
    03  FILLER PIC X.
    03  ITEM-D.
        05  ITEM-E PIC 9(4) COMP.
    03  ITEM-F PIC X.

Table 5-3 compares the major-minor technique of storage allocation with the left-to-right technique that assigns locations to a group item before its subsidiary items. Note that major-minor storage allocation adds a fill byte before ITEM-D. This forces location equivalence with ITEM-E, which is explicitly aligned by the SYNCHRONIZED clause.

Table 5-3 Comparison of Major-Minor and Left-Right Locations
Data Item Major-Minor
Location
Left-Right
Location
ITEM-A 00 00
ITEM-B 00 00
ITEM-C 00 00
FILLER 02 02
ITEM-D 04 03
ITEM-E 04 03
ITEM-F 06 05

The following diagram also shows the storage allocation for the record ITEM-A in Table 5-3 using both techniques. A hyphen (-) represents fill bytes caused by explicit alignment; an asterisk (*) represents the FILLER data item.


Regardless of the record allocation technique, an elementary move always produces the expected result. For example:


MOVE ITEM-C TO ITEM-E

Effect on Group Moves

A group move may produce an unexpected result, as in the following two situations:

  • If ITEM-A of the major-minor location format is moved to ITEM-A of the left-right location format, the fill byte of the major-minor location format overlays the first byte of ITEM-E in the left-right location format; then the first byte of ITEM-E in the major-minor location format overlays the second byte of ITEM-E in the left-right location format, and the second byte of ITEM-E in the major-minor location format overlays ITEM-F in the left-right location format. Finally, ITEM-F in the major-minor location format is truncated.
  • A different set of unexpected results occurs if a group move is done in the reverse direction. If ITEM-A of the left-right location format is moved to ITEM-A of the major-minor location format, the first byte of ITEM-D of the left-right location format is moved to the fill byte of the major-minor location format. Then the second byte of ITEM-E in the left-right location format is moved to the first byte of ITEM-E in the major-minor location format, and ITEM-F of the left-right location format is moved to the second byte of ITEM-E in the major-minor location format. Finally, ITEM-F is filled with a space because of the padding rule.

Boundary Equivalence

Boundary equivalence forces a group item to a boundary determined by the alignment of its subordinate items.

Within a record, a group item aligns on a boundary as large as the forced alignment boundary of any data item that:

  • Is subordinate to the group
  • Redefines the group
  • Is subordinate to a data item that redefines the group

Refer to the HP COBOL User Manual chapter on alignment for more information about boundary equivalence.

Figure 5-4 shows how the compiler determines the boundary where each item begins when you specify the no-alignment compiler option.

Figure 5-4 Effect of Boundary and Location Equivalence Rules on Sample Record


Figure 5-5 graphically represents Figure 5-4. It shows the result of location and boundary equivalence applied to the description of record ITEM-A. A hyphen (-) indicates fill bytes.

Figure 5-5 Storage Allocation for Sample Record


Note the location of ITEM-D. Location equivalence requires only that it have the same location as ITEM-E, its first subordinate item. ITEM-E requires only 2-byte boundary alignment. However, another of ITEM-D's subordinate items, ITEM-F, contains ITEM-I, which must be aligned on a 4-byte boundary. Therefore, boundary equivalence forces ITEM-D to a 4-byte boundary as well, causing two fill bytes between ITEM-E and ITEM-F.

This example shows how boundary equivalence helps make group moves predictable:


 01    ITEM-A.
    03    ITEM-B.
        05    ITEM-C    PIC X.
        05    ITEM-D    PIC 9(8) COMP SYNC.
    03    ITEM-E        PIC X.
    03    ITEM-F.
        05    ITEM-G    PIC X.
        05    ITEM-H    PIC 9(8) COMP SYNC.
    03    ITEM-I        PIC XX.


Previous Next Contents Index