COBOL COBOL 2014 (ISO/IEC 1989:2014), built with GnuCOBOL 3.x ยท
โ verified by execution on 2026-08-16
In COBOL, you sometimes need to look at the exact same piece of memory in two different ways. For example, a date might be stored as an 8-digit number (20260810), but you also need to extract just the year (2026). Instead of allocating new variables and moving substrings around, COBOL provides a much more efficient mechanism: the REDEFINES clause.
The REDEFINES clause allows different data description entries to describe the exact same physical computer storage area. It effectively acts like a union type in modern languages, allowing memory overlay without any physical data movement or duplication.
Basic Syntax and Placement
The REDEFINES clause must immediately follow the data-name of the item you want to redefine. The level number of both items must be identical.
In this example, FULL-DATE occupies 8 bytes of memory. DATE-PARTS is declared with REDEFINES FULL-DATE, meaning it does not take up any new memory. Instead, it places its sub-items (YYYY, MM, DD) directly on top of the 8 bytes already allocated for FULL-DATE.
The Memory Map
To visualize what the compiler is doing, imagine the 8 bytes of memory side-by-side:
When you modify FULL-DATE, the values of YYYY, MM, and DD change instantly because they point to the exact same bytes in RAM.
Essential Rules
When working with REDEFINES, the compiler enforces strict syntax rules:
Level Numbers: The redefining item and the redefined item must have the identical level number (e.g., both must be 05). You cannot redefine an 05 item with a 10 item.
No VALUE Clause: You cannot use a VALUE clause on the redefining item or any of its subordinate entries. Initial values must be specified on the original item.
Length Constraints: The redefining item should not be larger in length than the original redefined item, unless at level 01. If it is smaller, it simply maps to the beginning of the memory space and leaves the trailing bytes unmapped.
Sequence: The REDEFINES clause must immediately follow the data-name of the item being redefined.
Type Casting: Alpha to Numeric
A powerful use case for REDEFINES is โtype casting.โ COBOL does not enforce strict typing at the memory level. If you have an alphanumeric field that happens to contain numbers, you can redefine it as a numeric field to perform math on it!
By redefining PIC X(4) as PIC 9(4), we trick the compiler into treating the string โ1234โ as a number, allowing us to safely execute the ADD statement.
Predicting Memory Overlays
What happens if you define a substring overlay and change part of it? Test your understanding of memory boundaries.
Predict the outputcobol
Read the code. What exactly will it print? Commit to an answer before you look.
Which of the following is true about the REDEFINES clause?
Reveal answer
It allows you to use a different data name and format to access the exact same physical memory area. โ REDEFINES does not allocate new memory or perform data conversion. It simply maps a new description onto existing memory bytes.
What is a major restriction of the REDEFINES clause regarding the VALUE clause?
Reveal answer
The redefining item cannot contain a VALUE clause. โ Standard COBOL prohibits the VALUE clause on a redefining item. The memory must be initialized by the original item being redefined.
If a redefining item is smaller than the original redefined item, what happens?
Reveal answer
It is perfectly valid; the redefining item just maps to the first portion of the original memory. โ A redefining item can be smaller than the original item. It maps starting from the first byte (left-aligned) of the original storage area.
Challenges
Challenge 1 +20 XP
Fix the syntax so that NUM-VIEW correctly redefines RAW-BYTES.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
Extract the year from the date record and display it using REDEFINES.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.
This developer tried to initialize the `NUM-VIEW` item with the value `1234`. However, `REDEFINES` items cannot have a `VALUE` clause. While some compilers throw a fatal error, GnuCOBOL simply ignores it, leaving the memory empty (spaces)! Run the code to observe the failure. Move the `VALUE` clause to the original item so the data actually gets initialized.
This code runs. It just does the wrong thing. Read it, find the defect,
fix it โ the tests below decide when you are right.
cobolโ solution verified at build time
COBOL has no in-browser runtime, so this one is pen-and-paper: work out what the
fixed code should be, then open the solution and compare. The expected output
below is real โ it came from compiling the solution with GnuCOBOL when this page
was built.