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.
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.
| Section | Who reserves storage | Persistence | Typical use |
|---|---|---|---|
| WORKING-STORAGE SECTION | Reserved by program | For duration of run | Program variables, constants, work areas |
| LINKAGE SECTION | Provided by caller | Defined by caller | Parameters, communication areas |
| LOCAL-STORAGE SECTION | Reserved per entry | Reinitialized each entry | Recursive or reentrant data |
| FILE SECTION | System file buffers | Per file open | FD record descriptions |
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.
1234567WORKING-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.
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.
123456789LINKAGE 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 (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.
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.
1. Which section reserves storage that persists for the entire run of the program?
2. Which section describes data passed in from the caller and does not reserve its own storage?