REDEFINES: Reinterpreting Storage

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.

cobol โœ“ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. DATEREDEF.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  DATE-RECORD.
           05  FULL-DATE       PIC 9(8) VALUE 20260810.
           05  DATE-PARTS REDEFINES FULL-DATE.
               10  YYYY        PIC 9(4).
               10  MM          PIC 9(2).
               10  DD          PIC 9(2).
       PROCEDURE DIVISION.
           DISPLAY YYYY "-" MM "-" DD.
           STOP RUN.
Output
2026-08-10

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:

2 0 2 6 0 8 1 0 05 FULL-DATE PIC 9(8) 10 YYYY 10 MM 10 DD

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:

  1. 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.
  2. 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.
  3. 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.
  4. 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!

cobol โœ“ verified output
       IDENTIFICATION DIVISION.
       PROGRAM-ID. TYPEC.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  ALPHA-TO-NUM.
           05  ALPHA-ITEM      PIC X(4) VALUE "1234".
           05  NUM-ITEM REDEFINES ALPHA-ITEM PIC 9(4).
       PROCEDURE DIVISION.
           ADD 100 TO NUM-ITEM.
           DISPLAY ALPHA-ITEM.
           STOP RUN.
Output
1334

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 output cobol

Read the code. What exactly will it print? Commit to an answer before you look.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. OVERLAYP.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  BASE-STRING PIC X(6) VALUE "ABCDEF".
       01  SUB-VIEW REDEFINES BASE-STRING.
           05 PART-1 PIC X(3).
           05 PART-2 PIC X(3).
       PROCEDURE DIVISION.
           MOVE "XYZ" TO PART-1.
           DISPLAY BASE-STRING.
           STOP RUN.
Output
XYZDEF

Check yourself

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.

  • Test 1 โ€” expects "0042\n"
Show solution (0 XP)
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CHAL1.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  DATA-BLOCK.
           05 RAW-BYTES PIC X(4) VALUE "0042".
           05 NUM-VIEW REDEFINES RAW-BYTES PIC 9(4).
       PROCEDURE DIVISION.
           DISPLAY NUM-VIEW.
           STOP RUN.

Challenge 2 +20 XP

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.

  • Test 1 โ€” expects "2026\n"
Show solution (0 XP)
       IDENTIFICATION DIVISION.
       PROGRAM-ID. CHAL2.
       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01  DATE-REC.
           05 FULL-DATE PIC 9(8) VALUE 20260810.
           05 DATE-PARTS REDEFINES FULL-DATE.
              10 YYYY PIC 9(4).
              10 MM   PIC 9(2).
              10 DD   PIC 9(2).
       PROCEDURE DIVISION.
           DISPLAY YYYY.
           STOP RUN.

๐Ÿž Bug Hunt +25 XP

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.

  • Test 1 โ€” expects "1234\n"
Need a hint? (โˆ’25% XP)

Move the `VALUE` clause to `BASE-STR`.

Show solution (0 XP)
IDENTIFICATION DIVISION.
PROGRAM-ID. BUG1.
DATA DIVISION.
WORKING-STORAGE SECTION.
01  RAW-DATA.
    05  BASE-STR PIC X(4) VALUE "1234".
    05  NUM-VIEW REDEFINES BASE-STR PIC 9(4).
PROCEDURE DIVISION.
    DISPLAY NUM-VIEW.
    STOP RUN.