The COBOL pointer is a safe, typized memory address handler, can be address by its variable name.
The pointers can be used in three different ways:
to point on an existing memory area occupied by a normally allocated variable
to occupy a new memory area and receive structures which have no allocated memory (dynamic memory mamagement)
hybrid (tp point on existing area, then to receive several structures without allocated memories)
The latter uses an existing allocated structure to get memory size and area for receiving other structures.
Using the USAGE POINTER clause on declaration creates a 8-byte memory address under z/OS which can point to any data structure. The pointer does not contain data, only an address.
Using the ADDRESS OF clause will refer to the memory address of a reference (pointer or variable or structure).
Using the SET ... TO ... command will point a reference to an existing memory area.
In this case the pointer will provide an alternative variable name of existing variables. Pointing happens in runtime.
Once the pointer is set on a variable, the pointer acts as a variable. Because changing the pointed target is possible during runtime, the same alternative variable name can be used for working on different memory areas, namely data. This can reduce code and increase simplicity.
EXAMPLE FOR POINTER ON VARIABLES:
Upon declaration, the BASED clause forces COBOL no to allocate memory are of the given variable or structure. That means that the structure will be known but without memory.
The CEEGTST is an LE (Language Environment) routine which:
allocates memory from the heap with given size (length)
renders the area to the given pointer
sends back execution code (feedback on success or error)
Based on these, in runtime:
the pointer gets a memory area
a data structure defined with BASED will be formed in the area
later, another data structure can be formed on the same area
once a structure gets memory, the variable of the structure will be accessable and data manageable.
This pointer usage is useful for:
dynamic memory
API calls
CICS communication
file management
In case the allocated memory is smaller than what the forming structure would require, compile-time or runtime error raises. In case the allocated memory is larger than what is need, the rest of the area will be out of use. Therefore, it is advised to allocate memory for the pointer to have enough room for all corresponding structures used in the program.
EXAMPLE FOR MEMORY ALLOCATION FOR POINTER, DEFINITIONS WITHOUT ALLOCATIONS:
EXAMPLE FOR STRUCTURE DEFINITION WITHOUT MEMORY ALLOCATION:
Because a structure is defined without memory allocation, the used pointer will present the allocation for the structure. This means that the total dynamic memory management is available by giving the same structure to different pointers which have allocated memories.
In such case the pointers will have different memory areas: different data but carrying the same structure. In other words, instead of instant memory allocation upon definition of variables carrying the same structure, only one structure definition is enough, and later via introducing pointers, variables can be defined and allocated (similar system to the type and var segments under Delphi).
EXAMPLE FOR STRUCTURE DEFINITION, MULTIPLE POINTERS WITH MEMORY ALLOCATION:
EXAMPLE FOR MEMORY ALLOCATION VIA EXISTING MEMORY AREA:
The REDEFINES will address the same memory area than the source data has. The source data can be pointed to the memory allocation of a pointer. In this case the original variable must be defined with BASED.
EXAMPLE FOR REDEFINES AND POINTER:
The CICS APIs ofter need pointers.
EXAMPLE FOR CICS AND POINTER:
The VSAM and QSAM records often arrive via pointer.
EXAMPLE FOR FILE MANAGEMENT AND POINTER:
EXAMPLE FOR LEVEL-88 AND POINTER: