Summary -

In this topic, we described about the below sections -

COBOL allocates the variables contiguously in the memory location to save them for other storage purposes.

However, there are some cases where the variable's memory allocation should start from the respective boundaries (means byte boundaries) to increase the computation efficiency of the program.

SYNCHRONIZED Clause used to allocate the variables at their respective natural memory boundaries.

SYNCHRONIZED Clause specifies the alignment of an elementary item on a natural boundary in storage.

While allocating elements at the boundaries, there might be some unused bytes between the boundary and the variable allocation ending. These unused bytes are called as Slack bytes. We will discuss this below.

SYNC and SYNCHRONIZED are the same and have the same impact.

The SYNCHRONIZED Clause is generally not required; however, it can improve the performance of binary items used in arithmetic on some systems.

If the SYNCHRONIZED Clause is specified for level-01 group items, every elementary item within the group item is synchronized.

The compiler aligns all level-01 entries on a doubleword boundary. The length of an elementary item is not affected by the SYNCHRONIZED Clause.

Syntax -

Synchronized Syntax

LEFT -

  • Specifies the elementary item is to be placed at the left character position of the natural boundary.
  • It is the default option with the SYNCHRONIZED clause.
  • If specified in the SYNCHRONIZED clause, it has no effect.

RIGHT -

  • Specifies that the elementary item is to be positioned so that it terminates on the right character position of the natural boundary.

Slack Bytes -

The memory bytes that are left unused between the memory allocations are called slack bytes.

For the binary items that are not on their natural boundaries, the compiler inserts slack bytes within a record to ensure all SYNCHRONIZED items are on their proper boundaries.

The slack bytes are usually added before the SYNCHRONIZED elementary item to start it from the boundary.

There are two types of slack bytes -

  • Slack bytes within records
  • Slack bytes between records

Slack bytes within records -

Slack bytes (unused character positions) are added before each synchronized item in the records. For example -

01 STUDENT.
   05 STUDENT-NO       PIC 9(02).
   05 STUDENT-NAME     PIC X(12) SYNC.
   05 STUDENT-GRADE    PIC 9(02) SYNC.
   05 STUDENT-CLASS    PIC X(03).

The compiler inserts the slack bytes after STUDENT-NO to start the next item STUDENT-NAME from the boundary. The declaration becomes after slack bytes is -

01 STUDENT.
   05 STUDENT-NO       PIC 9(02).
  [05 SLACK-BYTES      PIC XX.  INSERTED BY COMPILER]
   05 STUDENT-NAME     PIC X(12) SYNC.
   05 STUDENT-GRADE    PIC 9(02) SYNC.
   05 STUDENT-CLASS    PIC X(03).

The below diagram represents the memory allocation along with slack bytes -

Slack bytes within record

Slack bytes between records -

Slack bytes (unused character positions) added between blocked logical records. For example -

01 STUDENT.
  03 STUDENT-D OCCURS 2 TIMES.
   05 STUDENT-NO       PIC 9(02) SYNC.
   05 STUDENT-NAME     PIC X(12) SYNC.
   05 STUDENT-GRADE    PIC 9(02) SYNC.
   05 STUDENT-CLASS    PIC X(03).

The compiler inserts the slack bytes (3) after record STUDENT-D to start the next record STUDENT-D from the boundary. The declaration becomes after slack bytes is -

01 STUDENT.
  03 STUDENT-D OCCURS 2 TIMES.
   05 STUDENT-NO       PIC 9(02) SYNC
   05 STUDENT-NAME     PIC X(12) SYNC.
   05 STUDENT-GRADE    PIC 9(02) SYNC.
   05 STUDENT-CLASS    PIC X(03).
  [05 SLACK-BYTES      PIC X(03).  INSERTED BY COMPILER]

The below diagram represents the two-records memory allocation along with slack bytes -

Slack bytes between record

Practical Example -

Scenario - Below example describes how the SYNC clause used in COBOL programming.

Code -

Constant program Code
----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
***************************** Top of Data ******************************
       IDENTIFICATION DIVISION.                                         
       PROGRAM-ID. SYNCCOB.                                             
       AUTHOR. MTH.                                                     
                                                                        
       DATA DIVISION.                                                   
       WORKING-STORAGE SECTION.                                         
       01 STUDENT.                                                      
          05 STUDENT-NO            PIC 9(02) VALUE 01.                  
          05 STUDENT-NAME          PIC X(12) SYNC VALUE "EMPLOYEE1".    
          05 STUDENT-GRADE         PIC 9(02) SYNC VALUE 1.              
          05 STUDENT-CLASS         PIC X(03) VALUE "5TH".               
                                                                        
       PROCEDURE DIVISION.                                              
                                                                        
           DISPLAY STUDENT.                                             
           STOP RUN.                                                    
**************************** Bottom of Data ****************************