Stack and Heap concept in COBOL
> I am new to this group.
Well, hello and welcome to CLC!
> I am a bit confused about the memory allocation in COBOL.
Don't mind, others are confused, too &-))
> Till now i was under the impression that there is
> no dynamic allocation of memory in COBOL and the Working storage
> variables are allocated memory when the load module is loaded.
Yes, you are right. WORKING-STORAGE is allocated when the program is
executed for the first time in a thread and remains allocated (and in
its last used state) if you call the program again. So , variables in
WORKING-STORAGE behave like they where static variables of a C or PL/I
> But i found the following information about Working storage
> variables in an IBM manual:
> Usage of Stack and Heap Storage by Language Environment-Conforming
> Language Stack Heap
> COBOL Intrinsic functions WORKING-STORAGE variables
> Library routines
> LOCAL-STORAGE variables(1)
OK, now we are talking of LOCAL-STORAGE instead of WORKING-STORAGE.
Variables which are declared in the LOCAL-STORAGE Section are
allocated in the STACK rather than in the HEAP. They are intialized at
every entry of the program and freed as soon as the program reaches
You will not be too surprised if I tell you that this kind of
variables is called automatic in C and PL/I. You need LOCAL-STORAGE
variables if you need a COBOL program to be called recursive (and of
course you need at least one variable in WORKING-STORAGE to be able to
stop the recursion).
OK, and the library routines and the intrinsic functions use the STACK
too. Probably, these routines are written in C and use automatic
storage. No problem, let them use what they have to use!
> STACK Used by library routine stack frames that can reside
> anywhere in storage
I'd guess you are now in another chapter of the manual. In which
> HEAP Allocates storage for user-controlled dynamically
> allocated variables
If a user-program ALLOCATEs storage, it gets HEAP storage. In COBOL,
there is no verb for ALLOCATE, you have to CALL CEEGTST.
> Could anyone help me with understanding this?
OK, now exactly WHAT are you trying to understand? If you are aware of
the (writable) static and the automatic concept in C, you should be
happy to see that COBOL is able to handle these kind of storage too.
But, to be honest, most people know that in COBOL all variables are in
its last used state, so they forget about the VALUE clause and they
are just happy.
> Thanks in advance,
> Shyam Menon.