Page 1 of 1

Local storage section in COBOL?

Posted: Sat Jul 25, 2015 8:53 pm
by Anu Agarwal
Hi,

What is local storage section in COBOL?

My answer was Working-Storage Section, but interviewr was did not say if it was correct or wrong! Please suggest your answer.

Re: Local storage section in COBOL?

Posted: Sat Jul 25, 2015 9:04 pm
by Robert Sample
WORKING-STORAGE and LOCAL-STORAGE are different. For a threaded application, there is a single copy of WORKING-STORAGE shared by all threads, while each thread has its own copy of LOCAL-STORAGE.

Re: Local storage section in COBOL?

Posted: Sat Jul 25, 2015 11:29 pm
by Anu Agarwal
But we do not code a LOCAL-STORAGE in a COBOL program ..right?

Re: Local storage section in COBOL?

Posted: Sun Jul 26, 2015 12:54 am
by Robert Sample
You may not yet have needed to code a LOCAL-STORAGE section but plenty of people have. It comes in handy in CICS programs or COBOL programs interacting with JAVA.

Re: Local storage section in COBOL?

Posted: Sun Jul 26, 2015 9:47 am
by Anuj Dhawan
The following example uses both WORKING-STORAGE and LOCAL-STORAGE. I've taken this example from here: http://publibz.boulder.ibm.com/cgi-bin/ ... 36#HDRWQ38

Code: Select all

     CBL pgmn(lu)
     *********************************
     * Recursive Program - Factorials
     *********************************
      IDENTIFICATION DIVISION.
      Program-Id. factorial recursive.
      ENVIRONMENT DIVISION.
      DATA DIVISION.
      Working-Storage Section.
      01  numb  pic 9(4)  value 5.
      01  fact  pic 9(8)  value 0.
      Local-Storage Section.
      01  num  pic 9(4).
      PROCEDURE DIVISION.
          move numb to num.
          if numb = 0
             move 1 to fact
          else
             subtract 1 from numb
             call 'factorial'
             multiply num by fact
          end-if.
          display num '! = ' fact.
          goback.
      End Program factorial.
The program produces the following output:

Code: Select all

     0000! = 00000001
     0001! = 00000001
     0002! = 00000002
     0003! = 00000006
     0004! = 00000024
     0005! = 00000120
There is some more explanation available at this link: http://publibz.boulder.ibm.com/cgi-bin/ ... HDRHDRVERK
WORKING-STORAGE for programs is allocated at the start of the run unit. Any data items with VALUE clauses are initialized to the appropriate value at that time. For the duration of the run unit, WORKING-STORAGE items persist in their last-used state. Exceptions are:
  1. A program with INITIAL specified in the PROGRAM-ID paragraph
    In this case, WORKING-STORAGE data items are reinitialized each time that the program is entered.
  2. A subprogram that is dynamically called and then canceled
    In this case, WORKING-STORAGE data items are reinitialized on the first reentry into the program following the CANCEL.
WORKING-STORAGE is deallocated at the termination of the run unit.
Hope this helps.

Re: Local storage section in COBOL?

Posted: Sun Jul 26, 2015 7:42 pm
by Anu Agarwal
Learned something new. Thanks guys.

Re: Local storage section in COBOL?

Posted: Tue Jul 28, 2015 11:07 pm
by William Collins
You define the LOCAL-STORAGE SECTION, part of the DATA DIVISION.

Each time a program is CALLed, the entire LOCAL-STORAGE is allocated and any data with VALUE clauses are set to those initial values.

When the CALL is ended (ie GOBACK is reached, amongst others) the LOCAL-STORAGE is deallocated (destroyed).

For a so-called "main" program, from EXEC PGM=..., the LOCAL-STORAGE is effectively equivalent to a second WORKING-STORAGE. You could use it if you "run out" of WORKING-STORAGE.

For an ordinary CALLed program, the LOCAL-STORAGE exists in the CALLed program during the CALL, and then ceases to exist. For a subsequent CALL, it is again allocated and initialised, and will again be destroyed when the CALLed program ends. This will happen each time the program is CALLed.

The IBM example posted by Anuj is an interesting example. Only one program, with one WORKING-STORAGE exists, but the first time it is CALLed and each time it CALLs itself another LOCAL-STORAGE is allocated. In the example, there will be a total of six LOCAL-STORAGEs for the program. As each GOBACK is encountered, the correct LOCAL-STORAGE is destroyed.

The purpose of the LOCAL-STORAGE in the example is to hold six distinct values, which are all referenced by the same name.

It is not necessarily a good example for someone who is "new" to LOCAL-STORAGE :-)