Memory Management Operations

The memory management operations are shown in the following table.

Table 69. Memory Management Operations
Operation Traditional Syntax Free-Form Syntax
Allocate Storage ALLOC (Allocate Storage) %ALLOC (Allocate Storage)
Free Storage DEALLOC (Free Storage)
Reallocate Storage REALLOC (Reallocate Storage with New Length) %REALLOC (Reallocate Storage)
Get the Address of a Variable %ADDR (Get Address of Variable)
Get the Address of a Procedure %PADDR (Get Procedure Address)

The ALLOC operation allocates heap storage and sets the result-field pointer to point to the storage. The storage is uninitialized.

The REALLOC operation changes the length of the heap storage pointed to by the result-field pointer. New storage is allocated and initialized to the value of the old storage. The data is truncated if the new size is smaller than the old size. If the new size is greater than the old size, the storage following the copied data is uninitialized. The old storage is released. The result-field pointer is set to point to the new storage.

The DEALLOC operation releases the heap storage that the result-field pointer is set to. If operational extender (N) is specified, the pointer is set to *NULL after a successful deallocation.

Storage is implicitly freed when the activation group ends. Setting LR on will not free any heap storage allocated by the module, but any pointers to heap storage will be lost.

There are two types of heap storage: single-level and teraspace. You can use the ALLOC keyword on the Control specification to control which type of heap storage is used by your memory management operations.

There are advantages and disadvantages of each type of heap storage.

For more information on the different types of heap storage, see the chapter on storage management in ILE Concepts.

Misuse of heap storage can cause problems. The following example illustrates a scenario to avoid:

D Fld1         S                   25A    BASED(Ptr1)
D Fld2         S                    5A    BASED(Ptr2)
D Ptr1         S                     *
D Ptr2         S                     *
 ....
C                 ALLOC     25               Ptr1
C                 DEALLOC                    Ptr1
 * After this point, Fld1 should not be accessed since the
 * basing pointer Ptr1 no longer points to allocated storage.
C                 CALL      'SOMEPGM'

 * During the previous call to 'SOMEPGM', several storage allocations
 * may have been done.  In any case, it is extremely dangerous to
 * make the following assignment, since 25 bytes of storage will
 * be filled with 'a'.  It is impossible to know what that storage
 * is currently being used for.
C                 EVAL      Fld1 = *ALL'a'

Following are more problematic situations:



[ Top of Page | Previous Page | Next Page | Contents | Index ]