Check pointer passed to function for validity, also non NULL case

306 Views Asked by At

Is there a way to check a pointer's validity besides the common assert(NULL != ptr)? I tried the following to intentionally provoke an "access error" so i will be alerted early, not lately when the pointer is accessed in some nested code:

#if !defined _NDEBUG || defined _DEBUG
  #define _check_ptr(ptr, type) do{type x; assert(NULL != ptr); x = *ptr;}while(0)
#else
  #define _check_ptr(ptr, type)
#endif

Desired usecase:

void func(int *ptr){
    _check_ptr(ptr, int);
    
    if (NULL != ptr){ // Still check in release
        ...
    }
}

As a precondition it would be perfectly acceptable to rely upon the IDE throwing me into the debugger on access error. In my case I will not run the debug build from command line or from somewhere outside the IDE.

0

There are 0 best solutions below