COBOLintermediate
REDEFINES Clause
Understand how REDEFINES lets multiple data descriptions share the same storage location and why this is both powerful and risky.
REDEFINES gives a second name and a second layout to the same bytes. No data is copied and nothing is converted.
REDEFINES is like a single sheet of graph paper viewed through two different stencils — one stencil reads it as a bar chart, the other as a table of numbers, but it's the same ink on the same paper the whole time.
Key Concepts
1
05 WS-DATE-NUM PIC 9(8).
05 WS-DATE-PARTS REDEFINES WS-DATE-NUM.
10 WS-YEAR PIC 9(4).
10 WS-MONTH PIC 9(2).
10 WS-DAY PIC 9(2).2
One eight-byte area. WS-DATE-NUM sees 20260315; WS-YEAR sees 2026. Write through one name and the other
sees the change immediately, because there is only one piece of storage.
WS-DATE-NUMWS-YEAR
3
It is a C union without the discriminator, and without any checking.
4
The main use: one record, several layouts. A transaction file where the record type decides the
structure:
5
01 TRANS-RECORD.
05 TRANS-TYPE PIC X(2).
05 TRANS-BODY PIC X(98).
05 PAYMENT-DETAIL REDEFINES TRANS-BODY.
10 PAY-AMOUNT PIC S9(9)V99 COMP-3.
10 PAY-METHOD PIC X(10).
...
05 ADDRESS-DETAIL REDEFINES TRANS-BODY.
10 ADDR-LINE-1 PIC X(40).
...6
Read the type, then use the matching view. No copying, no second record area.
7
Nothing enforces that you picked the right one. Read a payment record and look at ADDR-LINE-1 and you
get characters — the packed-decimal amount interpreted as text. No error, because bytes are bytes.
ADDR-LINE-1
8
The type code is the discriminator and the programmer is the only thing enforcing it.
9
The standard question: a COMP-3 field viewed as PIC X.
stored as COMP-3: 12 34 5C (the value 12345)
read as PIC X: unprintable charactersPIC X
10
People expect to see "12345". REDEFINES does not convert; it re-describes. To get a display value you
must MOVE it to a display field, which is a conversion.
REDEFINESMOVE
11
The rules. The redefining item must immediately follow the one it redefines, at the same level number.
It may be shorter and, in most implementations, may not extend beyond the original's storage. It cannot
carry a VALUE clause.
VALUE
12
And the caution worth stating. REDEFINES is powerful and defeats the type checking COBOL otherwise
gives you. Used for genuine multi-layout records it is the right tool; used to save a few bytes by
overlaying unrelated fields, it produces code where changing one thing breaks another for reasons nobody
can see.
REDEFINES
13
What the interviewer is probing.1. "What does REDEFINES actually do?" Probing: the mechanism. Stalls: "Copies data." *Moves
up:* it gives the same storage a second layout — no data is moved, so both names read the same
bytes.
14
2. "What is the legitimate use case?" Probing: judgement. Stalls: "Saving memory." *Moves
up:* a record whose layout varies by a type code, so each variant is a redefinition read after
checking the code.
15
3. "What is the danger?" Probing: the hazard. Stalls: "None." Moves up: reading through
the wrong redefinition gives garbage that looks like data, and packed fields read as display will
abend.
16
4. "What restriction applies to a REDEFINES entry?" Probing: the detail. Stalls: "None."
Moves up: it must be at the same level as the item it redefines and must immediately follow it,
and it cannot carry a VALUE clause.