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:

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.

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

The output would be

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.

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.

Ex: Invalid case, this can not be done

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.

Comments

  1. By Anupam

    Reply

Leave a Reply

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

Bitnami