Previous Topic Next topic Print topic


Pointer Data

A pointer value is the address of a variable's storage. For example:

DECLARE K FIXED BINARY(15); 
DECLARE A(5) FIXED BINARY(15); 
DECLARE P POINTER;
   .
   .
   .
P = ADDR(A(K));

In this example, P is a pointer variable that is capable of holding the address of any variable, with the possible exception of unaligned bit strings (for more information, see your Open PL/I User's Guide). The assignment statement uses the ADDR built-in function to calculate the address of A (K) and assigns that address as the value of P.

A pointer is used with a template to access the storage to which it points. For example:

DECLARE X FIXED BINARY(15) BASED;
   .
   .
   .
P->X = 10;

In this example, X is a based variable (that is, a template) describing a fixed-point binary integer. P is the pointer variable from the example at the beginning of this section. The assignment assigns 10 to A (K).

Pointers are often used to locate the storage of dynamically allocated based variables as described in the section Based Variables in the chapter Storage Classes, but may provide a mechanism for accessing another variable's storage, as shown in the previous two examples.

The null pointer value is produced by the NULL built-in function. It is a unique value that does not address any variable and is used to indicate that a pointer variable does not currently address anything, as shown in the following example:

DECLARE CHAIN_HEAD POINTER;
   .
   .
   .
CHAIN_HEAD = NULL();

The internal representation of NULL is determined by the -setnull compiler option. For more information on compiler options, see your Open PL/I User's Guide.

If the variable whose storage is addressed by a pointer value is freed, the pointer value should no longer be used. Use of such a pointer to access the storage causes unpredictable results.

The ADDR built-in function must not be used to calculate the address of an unaligned bit data. Consequently, when such a restriction is imposed, pointer values never address unaligned bit-string data. However, all other data can always be addressed by a pointer, regardless of the addressing capabilities of the computer.

Note:

The based variable that is used as a template normally must have the same data type as the variable addressed by the pointer. Violations of this rule cause unpredictable results. Programs that violate this rule and produce "correct" results may fail when compiled with optimization enabled or may fail when moved to another implementation of PL/I.

A pointer value cannot be represented by a constant. However, the NULL built-in function can be used in any context, including an INITIAL attribute, where a constant might be desired.

Previous Topic Next topic Print topic