Variables declared as CONTROLLED are allocated only when you specify them in an ALLOCATE statement. A controlled variable remains allocated until a FREE statement that names the variable is encountered or until the end of the program.
Controlled variables are partially independent of the program block structure, but not completely. The scope of a controlled variable can be internal or external. When it is declared as internal, the scope of the variable is the block in which the variable is declared and any contained blocks. Any reference to a controlled variable that is not allocated is in error.
|
Abbreviation: CTL
In the following example, the variable X can be validly referred to within procedure B and that part of procedure A that follows execution of the CALL statement.
A: proc; dcl X controlled; call B;
·
·
·
B: proc; allocate X;
·
·
·
end B; end A;
Generally, controlled variables are useful when a program requires large data aggregates with adjustable extents. Statements in the following example allocate the exact storage required depending on the input data and free the storage when it is no longer required.
dcl A(M,N) ctl; get list(M,N); allocate A; get list(A);
·
·
·
free A;
This method is more efficient than the alternative of setting up a begin-block, because block activation and termination are not required.