The PTR_VALID function verifies the validity of its pointer arguments, or alternatively returns a vector of pointers to all the existing valid pointer heap variables.
If called with an pointer or array of pointers as its argument, PTR_VALID returns a byte array of the same size as the argument. Each element of the result is set to True (1) if the corresponding pointer in the argument refers to an existing valid heap variable, or to False (0) otherwise.
If called with an integer or array of integers as its argument and the CAST keyword is set, PTR_VALID returns an array of pointers. Each element of the result is a pointer to the heap variable indexed by the integer value. Integers used to index heap variables are shown in the output of the HELP and PRINT commands. This is useful primarily in programming/debugging when the you have lost a reference but see it with HELP and need to get a reference to it interactively in order to determine what it is and take steps to fix the code. See the "Examples" section below for an example.
If no argument is specified, PTR_VALID returns a vector of pointers to all existing valid pointer heap variables-- even if there are currently no pointers to the heap variable . This usage allows you to "reclaim" pointer heap variables to which all pointers have been lost. If no valid pointer heap variables exist, a scalar null pointer is returned.
To determine if a given pointer refers to a valid heap variable
To destroy all existing pointer heap variables:
You can use the CAST keyword to "reclaim" lost heap variables. For example:
In this case, the integer index to the heap variable is 2. If we reassign the variable A, we will "lose" the pointer, but the heap variable will still exist:
We can reclaim the lost heap variable using the CAST keyword: