Storage classes, allocation, and deallocation
Storage allocation is the process of associating an area of storage with a variable so that the data item(s) represented by the variable can be recorded internally. When storage is associated with a variable, the variable is allocated. The storage class determines the degree of storage control and the manner in which storage is allocated and freed.
Allocation for a given variable can take place statically (before the execution of the program) or dynamically (during execution). A variable that is allocated statically remains allocated for the duration of the application program. A variable that is allocated dynamically relinquishes its storage either upon the termination of the block containing that variable, or at an explicit request from the application.
The storage class assigned to a variable determines the degree of storage control applied to the variable's storage and the manner in which the variable's storage is allocated and freed. You assign the storage class by using its corresponding attribute in an explicit, implicit, or contextual declaration. There are four storage classes:
- AUTOMATIC
- Specifies
that storage is allocated upon each entry to the block that contains
the storage declaration. The storage is released when the block is
exited.
If the block is a procedure that is invoked recursively, the previously allocated storage is pushed down upon entry; the latest allocation of storage is popped up in a recursive procedure when each generation terminates. For information about push-down and pop-up stacking, see Recursive procedures.
- STATIC
- Specifies that storage is allocated when the program is loaded. The storage is not freed until program execution is completed. The storage for a fetched procedure is not freed until the procedure is released.
- CONTROLLED
- Specifies that you use the ALLOCATE and FREE statements to control the allocation and freeing of storage. Multiple allocations of the same controlled variable in the same program, without intervening freeing, stacks generations of the variable. You can access earlier generations only by freeing the later ones.
- BASED
- Like
CONTROLLED, specifies that you control storage allocation and freeing.
One difference is that multiple allocations are not stacked but are
available at any time. Each allocation can be identified by the value
of a pointer variable. Another difference is that based variables
can be associated with an area of storage and identified by the value
of an offset variable.
Based variables outside of areas can be allocated by the ALLOCATE built-in function and freed by the PLIFREE built-in subroutine. They can also be allocated by the AUTOMATIC built-in function; such allocated variables are freed automatically when the block in which they are allocated terminates.
Storage class attributes can be declared explicitly for element, array, and major structure and union variables. For array and major structure and union variables, the storage class declared for the variable applies to all of the elements in the array or structure or union.
- CONDITION conditions
- Defined data items
- Entry constants
- File constants
- Format constants
- Identifiers defined in the DEFINE statement
- Label constants
- Members of structures and unions
- Named constants
Allocation of storage for variables is managed by PL/I. You do not specify where in storage the allocation is to be made. You can, however, specify that a variable be allocated in an existing AREA.