Storage Classes
1. Automatic Storage
Unless declared to have another storage class most variables have the AUTOMATIC attribute.
e.g.
MAIN : PROC OPTIONS ( MAIN );
DCL 1 STRUCTURE,
2 (A,B,C) FIXED(5,2),
2 D FIXED(5);
DO WHILE(MORE_DATA);
….
….
END;
P1 : PROC;
DCL TABLE(100) CHAR(10);
….
END P1;
P2 ; PROC;
DCL LIST(500) FIXED;
…
END P2;
END MAIN ;
- When main calls P1
- Storage for STRUCTURE stays allocated.
- Storage for TABLE is activated.
- When main calls P2.
- Storage for TABLE is released prior to this call.
- Storage for LIST is allocated.
- This process is called dynamic allocation.
- Prologue:
- It performs the allocation of dynamic allocation. Compiler controls this routine by executing it as the first step in the routine.
- Epilogue:
- The release of main storage that has been allocated to automatic variables is handled by a routine called epilogue. It is executed as the final step before the termination of the block.
2. Static Storage
l In the previous example, if we wanted to store the value of TABLE before P1 is called 2nd time, so that the value of LIST does not overlay it, then we need to allocate the storage for table STATICALLY.
l Here the storage is allocated before the execution of the program and remains allocated throughput the execution of the program.
l Program constants and variables declared with EXTERNAL attribute have STATIC storage class attribute. File names default to EXTERNAL and thus info related to a file is stored in static storage area.
l Static and Dynamic variables can be initialized using the INIT attribute. Static variables however are initialized once and Dynamic variables are initialized in each and every invocation of the block.
3. Based Storage
3.1 Comparison between automatic, static and based
l With AUTOMATIC storage address of storage is determined upon entry into the block
l With STATIC storage address of storage is determined when the program is loaded in the main storage.
l With BASED storage the address is contained in a pointer variable.
DCL P POINTER;
DCL A(100) FIXED DEC(5) BASED(P);
Indicates that the address of the array A is determined by the contents of P; The contents of P have not been established yet;
3.2 The way of use based variable
- A pointer variable is a special type of variable used to point to data in main storage.
- Before a reference can be made to a based variable a value must be given to the pointer.
- This can be done in one of the following ways:
- By assignment of the value returned by the ADDR built in function.
DCL VALUE1 BIT(32) BASED (P);
DCL VALUE2 FLOAT(6);
P = ADDR(VALUE2);
- By assignment of the value of another pointer.
DCL (P,Q) POINTER;
P = ADDR(AREA);
Q = P;
- With the SET option of a READ or LOCATE statement.
READ FILE(INPUT) SET(P);
LOCATE FILE(OUTPUT) SET(Q);
- By an ALLOCATE statement.
DCL (P,Q) PTR;
DCL AREA CHAR(100) BASED(P);
ALLOCATE (AREA);
ALLOCATE(AREA) SET (Q);
3.3 Overlay defining
DCL A(100) FIXED BIN(31);
DCL B(50) FLOAT DEC(6) BASED(P);
DCL P POINTER;
P = ADDR(A);
Here B is overlay defined on A although A and B have different base, scale and precision.( not possible with DEF attribute.)
- A based variable is a description of data i.e. a pattern for which no storage space is reserved but that will be overlaid on data in storage pointed to by an associated pointer variable.
- If A is itself a based variable , the returned value is determined from the pointer variable declared within A( If this pointer variable contains no value, the value returned by ADDR is undefined.)
3.4 Special consideration for character strings with varying clause.
DCL FIELD CHAR(100) VAR;
DCL 1 STRUCTURE BASED(ADDR(FIELD)),
2 LENGTH FIXED BIN(15,0),
2 DATA CHAR(100);
- The Length field must be specified in the structure to accommodate the two-byte field that always preceds a variable length character string.
.3.5 Read file using pointer
DCL P PTR;
DCL 1 IN _REC BASED(P),
2 A PIC ’ 99’ ,
2 B CHAR(8),
2 C FIXED DEC(7,2),
2 REST_OF_RECORD CHAR(66);
READ FILE(TAPEIN) SET(P);
- The read stmt. Causes a block of data to be read from the file named TAPEIN to an input buffer.
- It then Sets the pointer variable named in the set option to point to the location (starting address) of the next record.
Note: A Based variable shouldn’t describe more storage than the identifier on which it is based uses.
4. Controlled Storage
l Storage is allocated upon execution of ALLOCATE statement.
l Storage remains allocated until another statement FREE is executed.
l The allocation and freeing of controlled variable is under the complete control of the programmer.
e.g.
DCL A(100) FIXED DEC(5) CONTROLLED; /* A does not exist here */
ALLOCATE A; /*Storage for A allocated*/
GET LIST(A);
TOTAL = SUM(A);
FREE A; /*Storage for A dellocated*/