# Software Engineers Handbook/Language Dictionary/PLI/storage classes

Note: This article requires basic knowledge of PL/I which can be found in Software Engineers Handbook/Language Dictionary/PLI.
PL/I offers 4 different storage classes:
* STATIC
* AUTOMATIC (abbr: AUTO)
* CONTROLLED (abbr: CTL)
* BASED
Simplified spoken static and automatic variables will be allocated and initialised "by the machine", controlled and based variables have to be managed "by the program(mer)".

### Static and Automatic Variables

Static variables will be allocated and initialised at program start, they remain in storage until program termination$^{[1]}$. As a consequence of this a static variable which is declared local within a procedure will not loose its value between successive invocations of this procedure.
Automatic variables which are declared global behaves like static variables.
Automatic variables which are declared local within a procedure will be allocated and initialised at every invocation of this procedure, their storage will be released if this procedure terminates. Local variables are automatic by default$^{[2]}$.

### Controlled Variables

If a variable is declared as Controlled the declaration only describes the structure of the variable but no storage allocation will be done automatically.
The acquirement of storage for a controlled variable must be done with a ALLOCATE statement (abbr: ALLOC)$^{[3]}$, the deallocation has to be done with a FREE statement$^{[4]}$.
A controlled variable may be allocated multiple but only the actual generation of it can be directly accessed.
Local variables in a procedure which are controlled remains allocated between successive invocations of this procedure.

#### Adjustable Bounds etc.

* Every declared bound, length or size of a controlled variable may be overwritten in the allocate statement.
* Using asterisks notation in a declaration forces explicid definition in the 1st allocate statement.
* Using asteriks notation in an allocate statement inherits the values of the actual generartion.

### Based Variables

If a variable is declared as Based the declaration only describes the structure of the variable but no storage allocation will be done automatically.
* The acquirement of storage for a based variable may be done with a ALLOCATE statement (abbr: ALLOC)$^{[3]}$, the deallocation may be done with a FREE statement$^{[4]}$.
* Alternatively a based variable may be mapped to still existing storage.

#### Allocating

In the following example the ALLOC statement ...
* acquires storage for the variable B_VAR
* stores the address of the aquired storage in B_PTR
A based variable may be allocated multiple times, every generation can be directly accessed through its individuell pointer.

#### Allocating with Adjustable Bounds etc.

A based variable may be a self-defining structure, i.e. a structure for which values for bound, length or size must be defined in the allocate statement.
To make e.g. a bound adjustable its fixed value in the declaration has to replaced with
* expr is a expression which will be calculated at allocation time and determines the storage value.
* structure_var is a variable within the based structure which stores the calculated value.

### Notes

* $^{[1]}$ local static variables in fetched procedures will only be allocated between the fetch and the release statement.
* $^{[2]}$ in PL/I default values may be altered using the DEFAULT statement.
* $^{[3]}$ both controlled and based variables can be allocated in the same statement (e.g.: alloc ctl_var_1 (20), ctl_var_2, based_var;).
* $^{[4]}$ both controlled and based variables can be freed in the same statement (e.g.: free ctl_var_1, ctl_var_2, based_var;).