cobol redefines clause with examples

This topic covers the redefines in COBOL & its usage, syntax, declaration, valid & Invalid redefines cases.
In COBOL, Redefines clause is used to share the common storage area among different data items/groups thus saves the memory and helps to optimize the usage of working storage area. Useful in cases where the two variables are not used at the same time. In many cases, there are working storage variables which are not used simultaneously so these can be redefined to save the memory .

For example in a report, a header & trailer are used only once but the detail record is used multiple times. So Header, Trailer and Detail records can be defined using redefines. Below are the few cases where it can be used.

1. This can be used at 01 level as well.
Ex:

01 VAR-1 PIC X(10) VALUE 'MAHENDER'.
01 VAR-2 REDEFINES VAR-1 PIC X(5).

Here Var-1 is the redefined and Var-2 is the redefining item.
Redefining item should not contain a value clause where as redefined item can have a value clause as shown above.

2. One or more redefines are permitted.

01 VAR-1 PIC X(10) VALUE 'MAHENDER'.
01 VAR-2 REDEFINES VAR-1 PIC X(5).
01 VAR-3 REDEFINES VAR-2 PIC X(4).

Here Var-3 redefined Var-2, we can also use Var-1 instead of Var-2.

01 VAR-1 PIC X(10) VALUE 'MAHENDER'
01 VAR-2 REDEFINES VAR-1 PIC X(5).
01 VAR-3 REDEFINES VAR-2 PIC X(8).

DISPLAY "VAR-1" VAR-1.
DISPLAY "VAR-2:" VAR-2.
DISPLAY "VAR-3:" VAR-3.

The output would be

VAR-1MAHENDER
VAR-2:MAHEN
VAR-3:MAHENDER

Notes:

  • Even we have redefined Var-3 of Pic X(8) with Var-2 of 5 bytes the var-3 value is of 8 chars not 5 chars.
  • Because it is a common space that is used by all the 3 variables, whatever the value is present it shows in all 3 fields.
  • But unacceptable results occur if we try to move Var-1 to Var-2 or vise verse.

3. Redefined and Redefining items may vary in the data types and lengths.

01 a pic x(9)
   02 b redefines a pic x(8).

4. Redefined Item(a) should not contain occurs clause, but it can be part of an occurs group item.
Ex:Valid case
It can be the sub level of an occurs clause, but it can not redefine the item with occurs clause.

01 VAR-1.
   05 VAR-2-GRP OCCURS 10 TIMES.
      07 VAR2-A PIC X(5) VALUE 'REDDY'.
      07 VAR2-B REDEFINES VAR2-A PIC X(3).

Ex: Invalid case, this can not be done

01 VAR-1.
   05 VAR-2-GRP OCCURS 10 TIMES.
      07 VAR2-A PIC X(5) VALUE 'REDDY'.
      07 VAR2-B PIC X(3).
   05 VAR-3-GRP REDEFINES VAR-2-GRP PIC X(50).

If we declare like that compiler throws an error ‘A definition of an object of a “REDEFINES” clause contained an “OCCURS” clause. The “REDEFINES” clause was discarded’.

5. Redefines(ReDefs) in File Section.

It is possible in the FD section of file control but it is the implicit redefinition. If we declare the multiple file declarations(more than one 01 level entries) in the FD, then the ReDefs applies implicitly i:e the first 01 level storage will be allocated to next level.

3 Comments

Add a Comment

Your email address will not be published. Required fields are marked *

Close Bitnami banner