I saw this discussion - Checking for a null object in C++ and I was surprised that no one talked about when reference can point to a null object. In our code, we use null objects routinely. There are functions as follows which return nullObj.
const Obj&
nullObj()
{
static obj* nullPtr = NULL;
return static_cast< const Obj&>(*nullPtr);
}
Actually, when I looked at the code again to bring this topic up, I had some questions on how the above code works:
How is it possible to do *nullPtr
- Is is it because nullPtr is a static object, which is allocated memory on the heap and hence it is guaranteed to have some space and
Since we are returning const reference to obj, does compiler create a temporary object (to some kind of nullObj??) or Will the const reference act as an alias to nullPtr itself?
A reference shall be initialized to refer to a valid object or function. [Note: in particular, a null reference cannot exist in a well-defined program, because the only way to create such a reference would be to bind it to the “object” obtained by dereferencing a null pointer, which causes undefined behavior.
Dereferencing a null pointer always results in undefined behavior and can cause crashes. If the compiler finds a pointer dereference, it treats that pointer as nonnull. As a result, the optimizer may remove null equality checks for dereferenced pointers.
No, references cannot be NULL in C++.
Description. In Salesforce CPQ, the error 'Attempt to de-reference null object' is caused by a line of code that is searching for a value that does not exist because a particular field value is null. This error can also occur when a required Look-up field is missing a record value.
I was surprised that no one talked about when reference can point to a null object
That's because it can't, in a correct program.
Your function that dereferences a nullpointer has Undefined Behavior. The compiler is allowed to emit code that, for example, causes a crash at that point.
However, one possible effect of UB is that the code does what one thought it would do. So null-references can occur. I have never encountered one, but if you do, then it means that there is a serious logic error in the code.
All uses of the null-object-reference function you show, are logic errors.
You'd better grep up those uses and fix things. ;-)
Cheers & hth.,
I was surprised that no one talked about when reference can point to a null object.
Never.
(i) How is it possible to do *nullPtr - Is is it because nullPtr is a static object, which is allocated memory on the heap and hence it is guarenteed to have some space and address allocated for deref?
It's not. You're dereferencing a null pointer, which is invoking undefined behaviour.
(ii) Since we are returning const reference to obj, does compiler create a temporary object (to some kind of nullObj??) or Will the const reference act as an alias to nullPtr itself?
No. The compiler, at this stage, is allowed to produce nasal daemons or a black hole. If you're very lucky, you'll get a segmentation fault or some other kind of access violation.
DO NOT DO THIS
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With