View previous topic :: View next topic
|
Author |
Message |
umamageswari
New User
Joined: 26 Apr 2005 Posts: 1
|
|
|
|
Hi,
could any1 help me in bringing the Difference between satic and controlled storage clss in PL1.
Many thanks |
|
Back to top |
|
|
NitinD
New User
Joined: 03 May 2005 Posts: 3
|
|
|
|
Static Storage
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);
B: proc;
declare X static internal;
end B;
end A;
C: proc;
Y = 'hello';
end C;
Controlled storage
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.
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. |
|
Back to top |
|
|
j_prameela2000
New User
Joined: 01 Jun 2005 Posts: 28 Location: Chennai
|
|
|
|
Static Storage:
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.
Eg :
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.
Eg.
DCL A(100) FIXED DECIMAL(5) CONTROLLED;
ALLOCATE A;
GET LIST(A);
TOTAL = SUM(A);
FREE 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.
Eg :
DCL ARRAY(*,*) FIXED DECIMAL(5) CONTROLLED;
GET LIST(I,J);
ALLOCATE ARRAY(I,J);
Here * is used to indicate number of dimensions. |
|
Back to top |
|
|
sudheer648
New User
Joined: 23 May 2005 Posts: 97 Location: Chennai
|
|
|
|
Hi Pramella,
1.If there is no associate Free Statement for an Allocate Statement in the Program the memory allocated will be freed after the end of the program or not
2.Because language doesnt any gargabe collector to remove all the dangling memory pointers. |
|
Back to top |
|
|
j_prameela2000
New User
Joined: 01 Jun 2005 Posts: 28 Location: Chennai
|
|
|
|
Hi sudheer,
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.
Regards. |
|
Back to top |
|
|
|