Variables declared with the STATIC attribute are allocated prior to running a program. They remain allocated until the program terminates.
In the following example, the variable X is allocated for the life of the program, but it can be referenced only within procedure B or any block contained in B. The variable Y gets the STATIC attribute and is also allocated for the life of the program.
dcl Y char(1?);
A: proc options(main);
declare X static internal;
Y = 'hello';
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.
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.
dcl X controlled;
Generally, controlled variables are useful when a program requires large data aggregates with adjustable extents.
Whenever the value of a variable must be saved between different invocations of the same procedure, storage for that variable has to be allocated statically. The storage is allocated before execution of the program and remains allocated throughout the entire execution of the program. Static variables are initialized only once - before execution of a program begins. They are established with their respective values at a compile time.
DCL 1 STRUCTURE STATIC,
2 A FIXED DECIMAL(5,2),
2 B FIXED DECIMAL(5,2);
DCL TABLE(100) CHAR(10) STATIC;
Controlled Storage :
It is used to create a stack. The storage of controlled variables is allocated in the program by the ALLOCATE statement and released by the FREE statement.
DCL A(100) FIXED DECIMAL(5) CONTROLLED;
TOTAL = SUM(A);
A variable that has the CONTROLLED attribute is allocated upon the execution of an ALLOCATE statement specifying that identifier name. This allocation remains in effect even after the termination of the block in which it is allocated. Storage remains allocated until the execution of a FREE statement in which the identifier name is specified. The ALLOCATE statement may also be used to specify the amount of storage required for arrays if the array size is to be established during program execution rather than at compile time.
DCL ARRAY(*,*) FIXED DECIMAL(5) CONTROLLED;
Here * is used to indicate number of dimensions.
At the end of a program the allocated memory will get released. Consider an example :
I allocate x;
II allocate x;
If you want to access the firstly allocated x variable in the middle of the program unless and otherwise you release the memory storage of the secondly allocated x variable you cannot use the firstly allocated x variable since it is a stack (last in first out). Correct me if i am wrong.