Slack bytes within records
For any data description that has binary items that are not on their natural boundaries, the compiler inserts slack bytes within a record to ensure that all SYNCHRONIZED items are on their proper boundaries.
Because it is important that you know the length of the records in a file, you need to determine whether slack bytes are required and, if so, how many bytes the compiler will add. The algorithm that the compiler uses is as follows:
- The total number of bytes occupied by all elementary data items that precede the binary item are added together, including any slack bytes that are previously added.
- This sum is divided by m, where:
- m = 2 for binary items of four-digit length or less
- m = 4 for binary items of five-digit length or more and for COMPUTATIONAL-1 data items
- m = 4 or 8 for data items described with USAGE INDEX, USAGE POINTER, USAGE PROCEDURE-POINTER, or USAGE FUNCTION-POINTER
- m = 8 for COMPUTATIONAL-2 data items
- If the remainder (r) of this division is equal to zero, no slack bytes are required. If the remainder is not equal to zero, the number of slack bytes that must be added is equal to m - r.
These slack bytes are added to each record immediately following the elementary data item that precedes the binary item. They are defined as if they constitute an item with a level-number equal to that of the elementary item that immediately precedes the SYNCHRONIZED binary item, and are included in the size of the group that contains them.
For example:
01 FIELD-A.
05 FIELD-B PICTURE X(5).
05 FIELD-C.
10 FIELD-D PICTURE XX.
[10 SLACK-BYTES PICTURE X. INSERTED BY COMPILER]
10 FIELD-E COMPUTATIONAL PICTURE S9(6) SYNC.
01 FIELD-L.
05 FIELD-M PICTURE X(5).
05 FIELD-N PICTURE XX.
[05 SLACK-BYTES PICTURE X. INSERTED BY COMPILER]
05 FIELD-O.
10 FIELD-P COMPUTATIONAL PICTURE S9(6) SYNC.
Slack bytes can also be added by the compiler when a group item is defined with an OCCURS clause and contains within it a SYNCHRONIZED binary data item. To determine whether slack bytes are to be added, the following action is taken:
- The compiler calculates the size of the group, including all the necessary slack bytes within a record.
- This sum is divided by the largest m required by any elementary item within the group.
- If r is equal to zero, no slack bytes are required. If r is not equal to zero, m - r slack bytes must be added.
The slack bytes are inserted at the end of each occurrence of the group item that contains the OCCURS clause. For example, a record defined as follows appears in storage, as shown, in the figure after the record:
01 WORK-RECORD.
05 WORK-CODE PICTURE X.
05 COMP-TABLE OCCURS 10 TIMES.
10 COMP-TYPE PICTURE X.
[10 SLACK-BYTES PIC XX. INSERTED BY COMPILER]
10 COMP-PAY PICTURE S9(4)V99 COMP SYNC.
10 COMP-HOURS PICTURE S9(3) COMP SYNC.
10 COMP-NAME PICTURE X(5).


In
order to align COMP-PAY
and COMP-HOURS
on
their proper boundaries, the compiler added 2 slack bytes within the
record.
In the previous example, without further adjustment,
the second occurrence of COMP-TABLE
would begin 1
byte before a doubleword boundary, and the alignment of COMP-PAY
and COMP-HOURS
would
not be valid for any occurrence of the table after the first. Therefore,
the compiler must add slack bytes at the end of the group, as though
the record had been written as follows:
01 WORK-RECORD.
05 WORK-CODE PICTURE X.
05 COMP-TABLE OCCURS 10 TIMES.
10 COMP-TYPE PICTURE X.
[10 SLACK-BYTES PIC XX. INSERTED BY COMPILER]
10 COMP-PAY PICTURE S9(4)V99 COMP SYNC.
10 COMP-HOURS PICTURE S9(3) COMP SYNC.
10 COMP-NAME PICTURE X(5).
[10 SLACK-BYTES PIC XX. INSERTED BY COMPILER]
In this example, the second and each succeeding occurrence
of COMP-TABLE
begins 1 byte beyond a doubleword boundary.
The storage layout for the first occurrence of COMP-TABLE
now
appears as shown in the following figure:
Each succeeding occurrence within the table will now begin at the same relative position as the first.