Similar to this question: XCode 6.3 Warning : Comparison of address of 'myObject' not equal to null pointer is always true
with C++, I found that previously working code for evaluating null pointers stopped working:
struct AStruct
{
int x, y;
char *name;
};
AStruct& GetStruct()
{
return *(AStruct*)0;
}
int main(int argc, const char * argv[]) {
AStruct& mys = GetStruct();
if ( ! &mys) {
printf("null pointer \n");
}
else
{
printf("NOT a null pointer\n");
}
return 0
}
This always prints out
NOT a null pointer
I've tried other ways of pointer-to-reference checking:
if ( &mys == NULL)
if ( &mys == nullptr )
None of these worked.
Then I noticed the warning:
Reference cannot be bound to dereferenced null pointer in well-defined C++ code; comparison may be assumed to always evaluate to false
But there are no suggested fixes.
What is the canonical way of checking null pointers these days?