Local storage section in COBOL?
-
- New Member
- Posts: 3
- Joined: Tue Dec 02, 2014 9:14 pm
Local storage section in COBOL?
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.
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.
- Robert Sample
- Global Moderator
- Posts: 1895
- Joined: Fri Jun 28, 2013 1:22 am
- Location: Dubuque Iowa
Re: Local storage section in COBOL?
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.
-
- New Member
- Posts: 3
- Joined: Tue Dec 02, 2014 9:14 pm
- Robert Sample
- Global Moderator
- Posts: 1895
- Joined: Fri Jun 28, 2013 1:22 am
- Location: Dubuque Iowa
Re: Local storage section in COBOL?
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.
- Anuj Dhawan
- Founder
- Posts: 2802
- Joined: Sun Apr 21, 2013 7:40 pm
- Location: Mumbai, India
- Contact:
Re: Local storage section in COBOL?
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
The program produces the following output:
There is some more explanation available at this link: http://publibz.boulder.ibm.com/cgi-bin/ ... HDRHDRVERK
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.
Code: Select all
0000! = 00000001
0001! = 00000001
0002! = 00000002
0003! = 00000006
0004! = 00000024
0005! = 00000120
Hope this helps.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:
WORKING-STORAGE is deallocated at the termination of the run unit.
- 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.- 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.
Thanks,
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
Anuj
Disclaimer: My comments on this website are my own and do not represent the opinions or suggestions of any other person or business entity, in any way.
-
- New Member
- Posts: 3
- Joined: Tue Dec 02, 2014 9:14 pm
-
- Global Moderator
- Posts: 490
- Joined: Sun Aug 25, 2013 7:24 pm
Re: Local storage section in COBOL?
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 :-)
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 :-)
Create an account or sign in to join the discussion
You need to be a member in order to post a reply
Create an account
Not a member? register to join our community
Members can start their own topics & subscribe to topics
It’s free and only takes a minute