COBOL Tutorial

Progress0 of 0 lessons

COBOL LOCAL-STORAGE SECTION

The LOCAL-STORAGE SECTION is part of the DATA DIVISION. Data items you define there are reinitialized each time the program is entered (each time it is called). That makes LOCAL-STORAGE suitable for work areas and temporary data that should start fresh on every invocation, and for programs that must be reentrant. This page explains how LOCAL-STORAGE works and when to use it instead of WORKING-STORAGE.

Explain Like I'm Five: LOCAL-STORAGE vs WORKING-STORAGE

WORKING-STORAGE is like a desk you keep between visits: things stay where you left them. LOCAL-STORAGE is like getting a new desk every time you enter the room: each time the program is called, you get a clean desk with the same starting setup (VALUE clauses). So if you want something to remember from call to call, use the "persistent desk" (WORKING-STORAGE). If you want a clean slate every time, use the "new desk" (LOCAL-STORAGE).

How LOCAL-STORAGE Works

When the program is called, storage for LOCAL-STORAGE items is allocated (or made active), and each item is initialized according to its VALUE clause (or to default values if no VALUE is given). When the program exits (e.g. GOBACK, EXIT PROGRAM, STOP RUN), that storage is no longer in use for that invocation. On the next call, the program gets LOCAL-STORAGE again and it is reinitialized. So values in LOCAL-STORAGE do not carry over from one call to the next.

WORKING-STORAGE vs LOCAL-STORAGE
AspectWORKING-STORAGELOCAL-STORAGE
When initializedOnce at run-unit start (or when program first loaded)Every time the program is called
When values persistUntil run unit ends or program CANCELed/INITIALOnly for the duration of that call
Typical useState across calls, accumulators, flagsFresh work areas, reentrant programs

Syntax and Placement

LOCAL-STORAGE SECTION appears in the DATA DIVISION, typically after WORKING-STORAGE SECTION and before LINKAGE SECTION. You code it like WORKING-STORAGE: level numbers, data names, PICTURE (or USAGE POINTER, etc.), and VALUE clauses. The difference is not the syntax but the lifetime and reinitialization behavior.

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
DATA DIVISION. WORKING-STORAGE SECTION. 01 WS-CALL-COUNT PIC 9(5) VALUE ZERO. *> Persists across calls LOCAL-STORAGE SECTION. 01 LS-WORK-AREA. 05 LS-TEMP PIC X(80). 05 LS-LEN PIC 9(4) VALUE 0. *> Fresh on each call LINKAGE SECTION. 01 LK-PARAM PIC X(100). PROCEDURE DIVISION USING LK-PARAM. ADD 1 TO WS-CALL-COUNT *> Keeps count across calls MOVE SPACES TO LS-TEMP *> LS- items already reinitialized *> ... use LS-WORK-AREA ... GOBACK.

When to Use LOCAL-STORAGE

  • Called subprograms that need fresh work areas on every call and should not retain state.
  • Reentrant programs: LOCAL-STORAGE is per invocation, so each invocation has its own copy.
  • Temporary buffers or work fields that you do not want to leave lying around for the next call.
  • When you want to avoid explicit INITIALIZE or MOVE at the start of the procedure; VALUE in LOCAL-STORAGE does it for you each time.

When to Use WORKING-STORAGE

  • Data that must persist across calls: call counters, flags, caches, or accumulators.
  • Main program (top-level) data that is not re-entered; persistence is usually what you want.
  • When LOCAL-STORAGE is not available or not desired on your platform.

Step-by-Step: Adding LOCAL-STORAGE to a Called Program

  • In the DATA DIVISION, add LOCAL-STORAGE SECTION after WORKING-STORAGE (if any).
  • Define data items that should be reinitialized on every call (work areas, temps) in LOCAL-STORAGE with appropriate VALUE clauses.
  • Leave in WORKING-STORAGE any items that must keep their values between calls.
  • Run and test: call the program multiple times and verify LOCAL-STORAGE items start fresh and WORKING-STORAGE items persist as expected.

Test Your Knowledge

1. When is LOCAL-STORAGE reinitialized?

  • Only at program load
  • Each time the program is called
  • Only when CANCEL is used
  • Never

2. You want a counter that stays across multiple CALLs to your program. Where should you define it?

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

Related Concepts

Related Pages