Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Slack bytes and data alignment

Status
Not open for further replies.

rgkannan

Programmer
Feb 20, 2003
2
IN
What is slack bytes
How is the data aligned in cobol language?
 
In COBOL, 01 and 77 entries are aligned on a full-word or double-word boundary, depending on the platform, vendor, and user-parmeter specifications.

Data inside 01 levels are not aligned unless specified by the programmer with the SYNC or SYNCHRONIZED clause in the data description. Generally, SYNC only applies to binary and floating-point (COMP-1 & COMP-2) data. Half-word (2-byte) data is aligned on a half-word boundary. Full-word (4-byte) and Double-word (8-byte) data is aligned on a full-word boundary.

Slack bytes are "filler" bytes inserted by the compiler to effect the alignment. Consider the following example:
Code:
    01  GROUP-LEVEL.
        05  3-BYTE-STRING    PIC X(03).
        05  FULL-WORD-BINARY PIC 9(09) COMP SYNC.
GROUP-LEVEL is aligned on a full- or double-word boundary. the next byte after 3-BYTE-STRING is the end of a full-word. So COBOL ignores that byte and starts FULL-WORD-BINARY at the following byte, the beginning of a full word.

So the effective code is as follows:
Code:
    01  GROUP-LEVEL.
        05  3-BYTE-STRING    PIC X(03).
        05  filler           pic x(01).  *> slack byte
        05  FULL-WORD-BINARY PIC 9(09) COMP SYNC.

Note that GROUP-LEVEL is 8 bytes long. Without the SYNC it would have been 7 bytes long.

SYNC is not very useful any more. In the early days, the COBOL compiler would have to insert extra code to handle the mis-alignment of binary data. Most modern computers handle it in microcode.

It may still speed up the processing in extreme cases.
 
Hi Wer,
Suppose we have coded like this
01 GROUP-LEVEL.
05 3-BYTE-STRING PIC X(03).
05 FULL-WORD-BINARY PIC 9(09) COMP SYNC.

Then as you said the compiler inorder to align the data in the fulloword address, will insert a slack byte of i byte between 3-BYTE-STRING and FULL-WORD-BINARY. Suppose if i pass this group to a sub program, then in the subprogram's linkage section should i have to deliberately place a filler variable to accomodate that slack byte or it will be taken care of automatically?
Thanks,
kannan
 
If you code the data description in the subprograms's linkage section the same way with SYNC, the compiler will insert the slack byte in the same way.

But for new programs you should avoid slack bytes. As gregsimpson says, they are sloppy.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top