Creating Heap Variables

Heap variables can be created only by the pointer creation function PTR_NEW or the object creation function OBJ_NEW. (See Creating Custom Objects in IDL (Object Programming) for a discussion of object creation.) Copying a pointer or object reference does not create a new heap variable. This is markedly different from the way IDL handles "regular" variables. For example, with the statement:

A = 1.0 

you create a new IDL floating-point variable with a value of 1.0. The following statement:

B = A 

creates a second variable with the same type and value as A.

In contrast, if you create a new heap variable with the following command:

C = PTR_NEW(2.0d) 

the variable C contains not the double-precision floating-point value 2.0, but a pointer to a heap variable that contains that value. Copying the variable C with the following statement:

D = C 

does not create another heap variable, but rather creates a second pointer to the same heap variable. In this example, the HELP command would reveal:

% At  $MAIN$ 
A               FLOAT     =       1.00000 
B               FLOAT     =       1.00000 
C               POINTER   = <PtrHeapVar1> 
D               POINTER   = <PtrHeapVar1> 

The variables C and D are both pointers to the same heap variable. (The actual name assigned to a heap variable is arbitrary.) Changing the value stored in the heap variable would be reflected when dereferencing either C or D (dereferencing is discussed in Dereference).

Destroying or redefining either C, D, or both variables would leave the contents of the heap variable unchanged. When all pointers or references to a given heap variable are destroyed, the heap variable still exists and holds whatever memory has been allocated for it. See Heap Variable Leakage for further discussion. If the heap variable itself is destroyed, pointers to the heap variable may still exist, but will be invalid. See Dangling References.