Alignment by insertion of slack bytes
There are two types of slack bytes:
Intra-record slack bytes (slack bytes within records) are unused character positions which precede every aligned data item in the record.
Inter-record slack bytes (slack bytes between records) are unused character positions which are inserted between blocked logical records.
Intra-record slack bytes
For an output file or in the WORKING-STORAGE and LOCAL-STORAGE SECTION, the compiler inserts slack bytes within records to ensure that all aligned data items are justified on the appropriate boundaries. For an input file or in the LINKAGE section, the compiler expects any required slack bytes to be present in order to ensure proper alignment of a data item declared as SYNCHRONIZED.
Since it is very important for the user to know the length of a record in a file, the algorithm that the compiler uses to determine whether slack bytes are required and, if they are required, how many slack bytes are to be added, is described as follows:
The number of occupied bytes in all elementary data items which precede a data item in a record is computed, including any slack bytes previously added.
This sum is to be divided by m, where:
m = 2 for COMPUTATIONAL- or COMPUTATIONAL-5 or BINARY data items
with a length of 4 digits or less;m = 4 for COMPUTATIONAL or COMPUTATIONAL-5 or BINARY data items
with a length of 5 digits or more;m = 4 for COMPUTATIONAL-1 data items; m = 8 for COMPUTATIONAL-2 data items with a length of 18 digits or less; m = 4 for index data items; m = 8 for object references; m = 4 for pointers; m = 8 for typed group items. If the remainder r of this division is equal to zero, no slack bytes are required. If the remainder is unequal to zero, the number of slack bytes to be added is equal to m
-r
.These slack bytes are added to each record immediately following the elementary item that precedes the BINARY, COMPUTATIONAL, COMPUTATIONAL-1, COMPUTATIONAL-2, COMPUTATIONAL-5 or INDEX data item, the object reference, the pointer or the typed group item. They are declared as though they were a data item with a level number equal to that of the data item immediately preceding the aligned data item, and must be included in the size of the group where they are contained.
Create items which require alignment at the start of substructures. This ensures that they always contain the same number of slack bytes.
Example 2-9
Slack bytes within records
01 A. 02 B PICTURE X(5). 02 C. 03 D PICTURE XX. [03 slack byte PICTURE X. Inserted by the compiler.] 03 E PICTURE S9(6) COMP SYNCHRONIZED.
Slack bytes are also added by the compiler when a group item is described with an OCCURS clause and contains an aligned data item defined with USAGE as BINARY, COMPUTATIONAL, COMPUTATIONAL-1, COMPUTATIONAL-2, COMPUTATIONAL-5 or INDEX. To decide whether to add slack bytes, the following steps are performed:
The compiler calculates the size of the group including all intra-record slack bytes required.
This sum is divided by the largest m required by any elementary item within the group.
If the remainder r of this division is equal to zero, no slack bytes will be needed. If r is unequal to zero, m-r slack bytes must be added.
Insertion of slack bytes takes place at the end of each occurrence of the group item which contains the OCCURS clause, in order to ensure that all occurrences of table items begin at the same kind of boundary. In example 2-8 in section "Concept of computer-independent data description", all occurrences of D begin one byte beyond a double-word boundary.
Example 2-10
Occurrences of slack bytes in tables
01 A. 02 B PICTURE X. 02 C OCCURS 10 TIMES. 03 D PICTURE X. [03 slack bytes PICTURE XX. Inserted by the compiler.] 03 E PICTURE S9(4)V99 COMP SYNC. 03 F PICTURE S9(4) COMP SYNC. 03 G PICTURE X(5). [03 slack bytes PICTURE XX. Inserted by the compiler.]
If aligned data items defined as BINARY, COMPUTATIONAL, COMPUTATIONAL-1, COMPUTATIONAL-2, COMPUTATIONAL-5 or INDEX follow an entry with an OCCURS DEPENDING clause, then slack bytes are added on the basis of the item which is repeated with the maximum number. If the length of this item is not divisible by the m required by the data, then only certain values of the data-name used in the DEPENDING phrase produce a correct alignment of the items. The programmer should be aware of this situation and try to avoid it. These values are ones in which the length of the data item, multiplied by the number of occurrences plus the number of slack bytes calculated on the basis of the maximum number of occurrences, is divisible by m with no remainder.
Example 2-11
Occurrences of slack bytes in tables with the DEPENDING phrase
01 A. 02 B PICTURE 99. 02 C PICTURE X OCCURS 50 TO 99 TIMES DEPENDING ON B. [02 slack bytes PICTURE X. Inserted by the compiler.] 02 D PICTURE S99 COMP SYNC.
In this example, when references to D are required, B is restricted to odd values.
01 A. 02 B PICTURE 999. 02 C PICTURE XX OCCURS 20 TO 99 TIMES DEPENDING ON B. [02 slack bytes PICTURE X. Inserted by the compiler.] 02 D PICTURE S99 COMP SYNC.
In this example, all values of B provide correct references to D.
Alignment of records
All record descriptions (01-level entries) in all sections of the Data Division begin at doubleword boundaries.Inter-record slack bytes
When records that contain aligned data items are to be blocked, the programmer must ensure that all records following the first record in the input-output storage area are properly boundary-aligned. This is only necessary, however, in cases where data is to be processed blockwise (locate mode). COBOL2000 does not use this mode.