SYNC and alignment

A binary field is read by the processor as a unit of two, four or eight bytes, and some processors read it faster, or only, when it starts at an address divisible by its size. COBOL has a clause for that: SYNC. The compiler then inserts unused bytes in front of the field to move it to a boundary, and those bytes are part of the record.

A binary field wants an address

Two records, the same fields, one clause on one field. The key is one byte, the count is a nine-digit binary field of four bytes, and the flag is one byte:

IDENTIFICATION DIVISION. PROGRAM-ID. LAYOUT. DATA DIVISION. WORKING-STORAGE SECTION. 01 UNSYNCED. 05 U-KEY PIC X. 05 U-COUNT PIC 9(9) COMP. 05 U-FLAG PIC X. 01 SYNCED. 05 S-KEY PIC X. 05 S-COUNT PIC 9(9) COMP SYNC. 05 S-FLAG PIC X. PROCEDURE DIVISION. DISPLAY "unsynced " LENGTH OF UNSYNCED. DISPLAY "synced " LENGTH OF SYNCED. DISPLAY "u-count " LENGTH OF U-COUNT. DISPLAY "s-count " LENGTH OF S-COUNT. MOVE "A" TO U-KEY. MOVE 1 TO U-COUNT. MOVE "B" TO U-FLAG. DISPLAY "u hex " FUNCTION HEX-OF(UNSYNCED). MOVE "A" TO S-KEY. MOVE 1 TO S-COUNT. MOVE "B" TO S-FLAG. DISPLAY "s hex " FUNCTION HEX-OF(SYNCED). STOP RUN.

cobc -x -free -Wall -o layout layout.cob
./layout

unsynced 6
synced 9
u-count 4
s-count 4
u hex 410000000142
s hex 412020200000000142

The field is four bytes in both records, and the record grew by three. The hex shows where they went: after the key 41 come three bytes of 20, a space, before the count 00000001 and the flag 42. The count now starts at offset four, which is what its four bytes want, and the three bytes in front of it are storage that no field of the record describes.

How the padding is counted

The padding depends on the size of the field, because the boundary is the size of the field itself: a two-byte field wants an even offset, a four-byte field an offset divisible by four. The SYNCHRONIZED spelling is the same clause:

IDENTIFICATION DIVISION. PROGRAM-ID. SPELLED. DATA DIVISION. WORKING-STORAGE SECTION. 01 REC-A. 05 A-KEY PIC X. 05 A-BIN PIC S9(4) COMP SYNC. 05 A-TAIL PIC X(2). 01 REC-B. 05 B-KEY PIC X. 05 B-BIN PIC S9(4) COMP SYNCHRONIZED. 05 B-TAIL PIC X(2). 01 REC-C. 05 C-KEY PIC X. 05 C-BIN PIC S9(4) COMP. 05 C-TAIL PIC X(2). PROCEDURE DIVISION. DISPLAY "SYNC " LENGTH OF REC-A. DISPLAY "SYNCHR " LENGTH OF REC-B. DISPLAY "plain " LENGTH OF REC-C. STOP RUN.

cobc -x -free -Wall -o spelled spelled.cob
./spelled

SYNC 6
SYNCHR 6
plain 5

One byte of key and a two-byte field: the field starts at offset one, which is odd, so one byte of padding moves it to offset two. The record gains exactly that byte, and the spelled-out keyword gives the same layout.

Two fields in one record show the arithmetic adding up. Both binary fields are aligned, the four-byte one needs three bytes of padding after the key, the two-byte one is already at an even offset:

IDENTIFICATION DIVISION. PROGRAM-ID. TWO. DATA DIVISION. WORKING-STORAGE SECTION. 01 PLAIN-REC. 05 P-KEY PIC X. 05 P-BIG PIC 9(9) COMP. 05 P-SML PIC 9(4) COMP. 05 P-TAG PIC X. 01 SYNC-REC. 05 S-KEY PIC X. 05 S-BIG PIC 9(9) COMP SYNC. 05 S-SML PIC 9(4) COMP SYNC. 05 S-TAG PIC X. PROCEDURE DIVISION. DISPLAY "plain " LENGTH OF PLAIN-REC. DISPLAY "sync " LENGTH OF SYNC-REC. STOP RUN.

cobc -x -free -Wall -o two two.cob
./two

plain 8
sync 11

Eight bytes become eleven, and the three new ones are all in front of S-BIG. The tag did not move because it is one byte, and a one-byte field is always aligned.

Packed decimal does not move

COMP-3 has no alignment requirement: it is read as bytes, not as a word of the processor, so the clause changes nothing.

IDENTIFICATION DIVISION. PROGRAM-ID. PACK. DATA DIVISION. WORKING-STORAGE SECTION. 01 A-REC. 05 A-KEY PIC X. 05 A-AMT PIC S9(5)V99 COMP-3. 05 A-TAIL PIC X(2). 01 B-REC. 05 B-KEY PIC X. 05 B-AMT PIC S9(5)V99 COMP-3 SYNC. 05 B-TAIL PIC X(2). 01 C-REC SYNC. 05 C-KEY PIC X. 05 C-TXT PIC X(3). PROCEDURE DIVISION. DISPLAY "plain " LENGTH OF A-REC. DISPLAY "sync " LENGTH OF B-REC. DISPLAY "01 sync " LENGTH OF C-REC. STOP RUN.

cobc -x -free -Wall -o pack pack.cob
./pack

pack.cob:14: warning: ignoring SYNCHRONIZED for group item 'C-REC' [-Wadditional]
12 | 05 B-AMT PIC S9(5)V99 COMP-3 SYNC.
13 | 05 B-TAIL PIC X(2).
14 > 01 C-REC SYNC.
15 | 05 C-KEY PIC X.
16 | 05 C-TXT PIC X(3).
plain 7
sync 7

Seven bytes with the clause and seven without, because a packed field can start anywhere. The record with the clause on the group has the same length too, and the warning above the output is its own subject.

SYNC on a group is ignored

The clause belongs to a field with a picture. A group has no size of its own — it is its children — so there is nothing to align, and the compiler drops the clause and says so: warning: ignoring SYNCHRONIZED for group item 'C-REC' [-Wadditional]. The record is four bytes, the sum of one and three, and the warning is the only trace of the clause.

The same applies to a level 01 that carries SYNC at the end of its line, which is where the mistake usually appears: it looks like a statement about the record and is a statement about a field that does not exist. The alignment that matters is the one on the binary children, and each of them needs its own clause.

Why a file record must not use it

The record above grew from six bytes to nine because of a clause, and the number of bytes added depends on the size of the binary field, which depends on the compiler and on the picture. A file record with a SYNC in it has a layout that another machine may reproduce differently, and the receiving program reads every field after the padding from the wrong offset.

The rule that follows is simple and old. A record that leaves the program is described without SYNC, and if the field has to be binary the record says where it sits with REDEFINES or with filler bytes, so the layout is written in the data division and not left to the compiler. SYNC is for working storage that a native call needs, where nothing is written to a file and nothing is shared with a program built by another compiler.

The next part of the course moves to arithmetic, where the size of a field stops being a layout question and becomes a question about precision; ADD and SUBTRACT is the first step.

Exercises

  1. Give U-COUNT a SYNC and print the hex again. Which three bytes appear, and where?
  2. Put a PIC S9(4) COMP first in a record and a PIC X after it. Is there any padding, and why?
  3. Add SYNC to the two-byte field only in the two program and print the length. Explain the number from the offsets.
  4. Build the pack program with -Wall and then without it. Which of the two outputs changes, and why is that a reason to keep the flag on?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does a SYNC clause do to a binary field?

    Answer

    It moves the field to an aligned offset by inserting unused bytes

    Alignment is about the address of the field, so the compiler inserts bytes in front of it and the record grows.

  2. Why must a file record not carry SYNC?

    Answer

    The padding depends on the compiler and the machine, so the layout is not fixed

    A record that leaves the program has to mean the same bytes everywhere, and padding chosen by a compiler does not.

  3. What does SYNC change about the field itself?

    Answer

    Nothing: its length and value stay the same, only its offset changes

    The four bytes of a 9(9) COMP stay four; the record grows around the field, not the field.

  4. What happens when SYNC is written on a group item?

    Answer

    The compiler warns that it is ignoring the clause and builds the record unaligned

    A group has no size of its own, so there is no boundary to align; -Wall shows the warning that says the clause was dropped.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 SYNCED. 05 S-KEY PIC X. 05 S-COUNT PIC 9(9) COMP SYNC. 05 S-FLAG PIC X. PROCEDURE DIVISION. DISPLAY LENGTH OF SYNCED. STOP RUN.

    Answer

    9

    One byte of key, three bytes of padding to reach offset four, four bytes of count and one of flag.

  6. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 SYNCED. 05 S-KEY PIC X. 05 S-COUNT PIC 9(9) COMP SYNC. 05 S-FLAG PIC X. PROCEDURE DIVISION. MOVE "A" TO S-KEY. MOVE 1 TO S-COUNT. MOVE "B" TO S-FLAG. DISPLAY FUNCTION HEX-OF(SYNCED). STOP RUN.

    Answer

    412020200000000142

    The key is 41, three padding bytes of 20, the count 00000001 and the flag 42.

  7. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 REC-A. 05 A-KEY PIC X. 05 A-BIN PIC S9(4) COMP SYNC. 05 A-TAIL PIC X(2). PROCEDURE DIVISION. DISPLAY LENGTH OF REC-A. STOP RUN.

    Answer

    6

    The two-byte field is moved from offset one to offset two by a single padding byte, and the two-byte tail follows.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q4. DATA DIVISION. WORKING-STORAGE SECTION. 01 PLAIN-REC. 05 P-KEY PIC X. 05 P-BIG PIC 9(9) COMP. 05 P-SML PIC 9(4) COMP. 05 P-TAG PIC X. 01 SYNC-REC. 05 S-KEY PIC X. 05 S-BIG PIC 9(9) COMP SYNC. 05 S-SML PIC 9(4) COMP SYNC. 05 S-TAG PIC X. PROCEDURE DIVISION. DISPLAY LENGTH OF PLAIN-REC " " LENGTH OF SYNC-REC. STOP RUN.

    Answer

    8 11

    The plain record is one plus four plus two plus one; the synced one adds three bytes before the four-byte field, and the two-byte field needs none.

  9. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q5. DATA DIVISION. WORKING-STORAGE SECTION. 01 REC-A. 05 A-KEY PIC X. 05 A-AMT PIC S9(5)V99 COMP-3. 05 A-TAIL PIC X(2). 01 REC-B. 05 B-KEY PIC X. 05 B-AMT PIC S9(5)V99 COMP-3 SYNC. 05 B-TAIL PIC X(2). PROCEDURE DIVISION. DISPLAY LENGTH OF REC-A " " LENGTH OF REC-B. STOP RUN.

    Answer

    7 7

    Packed decimal has no alignment requirement, so SYNC adds nothing: one key, four packed bytes and two of tail.

  10. One program writes a six-byte record without SYNC and another reads it with SYNC on the binary field. What does the reader see?

    Answer

    Every field after the padding is read from the wrong offset

    The reader expects padding that the writer never wrote, so its layout is three bytes longer and each field after it lands on the wrong bytes.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev