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.

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 it and the manner in which the variable's storage is allocated and freed. There are four storage classes: automatic, static, controlled, and based. You assign the storage class using its corresponding attribute in an explicit, implicit, or contextual declaration:
  • 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 a discussion of 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 and freed using the ALLOCATE built-in function and PLIFREE built-in subroutine respectively. They can also be allocated using 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.

Storage class attributes cannot be specified for:
  • 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. For more information, refer to Area data and attribute.