Data Structure in C : Dangling References
If a heap variable is destroyed, any remaining pointer variable or object
reference that still refers to it is said to contain a dangling reference.
Unlike lower level languages such as C, dereferencing a dangling reference will
not crash or corrupt your IDL session. It will, however, fail with an error
message. For example:
23
;Create a new heap variable.
A = PTR_NEW(23)
;Print A and the value of the heap variable A points to.
PRINT, A, *A
IDL prints:
For example:
;Destroy the heap variable.
PTR_FREE, A
;Try to print again.
PRINT, A, *A
IDL prints:
% Invalid pointer: A.
% Execution halted at: $MAIN$
There are several possible approaches to avoiding such
errors. The best option is to structure your code
such that dangling
references do not occur. You can, however, verify the validity of pointers or
object references before using them (via the PTR_VALID or OBJ_VALID functions)
or use the CATCH mechanism to recover from the effect of such a dereference.
Comments
Post a Comment