COBOL Tutorial

Progress0 of 0 lessons

COBOL Storage Management

COBOL does not have pointers or malloc like C. Instead, it manages storage through named sections in the Data Division: WORKING-STORAGE, LINKAGE, LOCAL-STORAGE, and the FILE SECTION. Each section has a different purpose and lifetime. Knowing when to use which section helps you write correct, predictable programs and avoid subtle bugs from shared or leftover data.

Explain Like I'm Five: Where Does Data Live?

Imagine the program has different drawers: one drawer (WORKING-STORAGE) is the program's own and stays the whole time it runs; another (LINKAGE) is like a shared envelope—the caller puts data there and the program reads it; a third (LOCAL-STORAGE) is emptied and refilled each time someone enters the program. The FILE SECTION is like a tray the system uses when reading or writing files. Choosing the right drawer is storage management.

The Main Storage Sections

COBOL storage sections at a glance
SectionWho reserves storagePersistenceTypical use
WORKING-STORAGE SECTIONReserved by programFor duration of runProgram variables, constants, work areas
LINKAGE SECTIONProvided by callerDefined by callerParameters, communication areas
LOCAL-STORAGE SECTIONReserved per entryReinitialized each entryRecursive or reentrant data
FILE SECTIONSystem file buffersPer file openFD record descriptions

WORKING-STORAGE SECTION

WORKING-STORAGE SECTION is where you define the program's own variables and constants. The compiler and runtime reserve real storage for these items. Storage is typically allocated when the program starts and remains until the program ends. Values persist across paragraph and section executions, so a value you set in one paragraph is still there when you use it in another. You can use VALUE clauses to set initial values.

cobol
1
2
3
4
5
6
7
WORKING-STORAGE SECTION. 01 WS-COUNTER PIC 9(5) VALUE ZEROS. 01 WS-NAME PIC X(30). 01 WS-TOTAL PIC 9(7)V99. *> WS-COUNTER, WS-NAME, WS-TOTAL keep their values *> for the entire run of the program.

LINKAGE SECTION

The LINKAGE SECTION describes data that the caller passes to the program. No storage is reserved by your program—the caller provides the address(es). You are describing the layout (level numbers, PICTUREs) of that external data. You cannot use VALUE in the LINKAGE SECTION. LINKAGE is used for parameters on CALL, for CICS communication areas (COMMAREA), and for any data that is shared with another program or the environment.

cobol
1
2
3
4
5
6
7
8
9
LINKAGE SECTION. *> Describes data passed from caller; no VALUE allowed 01 LK-COMMAREA. 05 LK-USER-ID PIC X(8). 05 LK-ACTION PIC X(1). 05 LK-RESPONSE PIC 9(4). PROCEDURE DIVISION USING LK-COMMAREA. *> LK-COMMAREA points to storage provided by the caller.

LOCAL-STORAGE SECTION

LOCAL-STORAGE SECTION (when supported) provides storage that is reinitialized each time the program is entered. That is useful for recursive programs or when the program can be invoked multiple times and you do not want leftover values from a previous invocation. Not all compilers support LOCAL-STORAGE; check your manual.

FILE SECTION

The FILE SECTION contains the record descriptions (01-level and below) for each file defined in the Environment Division. These are the buffers used when you READ or WRITE a file. The system manages when they are used; you do not use them for general program variables.

Step-by-Step: Choosing the Right Section

  • Use WORKING-STORAGE for data that is private to the program and must persist for the whole run (counters, work fields, constants).
  • Use LINKAGE SECTION for parameters and communication areas that are passed in from the caller; do not use VALUE there.
  • Use LOCAL-STORAGE when you need fresh storage on each entry (e.g. reentrant or recursive use), if your compiler supports it.
  • Use the FILE SECTION only for record descriptions tied to SELECT/FD file definitions.

Test Your Knowledge

1. Which section reserves storage that persists for the entire run of the program?

  • LINKAGE SECTION
  • LOCAL-STORAGE SECTION
  • WORKING-STORAGE SECTION
  • FILE SECTION

2. Which section describes data passed in from the caller and does not reserve its own storage?

  • WORKING-STORAGE SECTION
  • LOCAL-STORAGE SECTION
  • LINKAGE SECTION
  • FILE SECTION

Related Concepts

Related Pages