CheckPointer Function

Overview

The CheckPointer function returns the type of an object pointer. It is crucial for validating pointer integrity before accessing the object to prevent critical program termination.

Signature

ENUM_POINTER_TYPE CheckPointer(object* anyobject);

Parameters

  • anyobject [in]: A pointer to an object.
  • Return Value

    Returns a value from the ENUM_POINTER_TYPE enumeration, indicating the pointer's validity.

    Notes

  • An invalid pointer (e.g., NULL or a pointer to a deleted object) will cause critical program termination if not checked.
  • Calling CheckPointer before using a pointer is recommended.
  • A non-zero return value guarantees that the pointer can be safely used for object access.
  • Related Functions

  • Alert
  • Comment
  • GetPointer
  • MessageBox
  • Print
  • ResetLastError
  • SetUserError
  • Sleep
  • ZeroMemory
  • Constants

  • ENUM_POINTER_TYPE (for return values)
  • Example

    //+------------------------------------------------------------------+
    //| Deletes list by deleting its elements                            |
    //+------------------------------------------------------------------+
    void CMyList::Destroy()
      {
    //--- service pointer for working in the loop
       CItem* item;
    //--- go through loop and try to delete dynamic pointers
       while(CheckPointer(m_items)!=POINTER_INVALID)
         {
          item=m_items;
          m_items=m_items.Next();
          if(CheckPointer(item)==POINTER_DYNAMIC)
            {
             Print("Dynamyc object ",item.Identifier()," to be deleted");
             delete (item);
            }
          else Print("Non-dynamic object ",item.Identifier()," cannot be deleted");
         }
    //---
      }
    

    See Also

  • [Object Pointers](/basis/types/object_pointers)
  • [Checking the Object Pointer](/constants/namedconstants/enum_pointer_type)
  • [Object Delete Operator delete](/basis/operators/deleteoperator)