Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference pointing to a Null-object

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:

  1. 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

  2. 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?

like image 519
cppcoder Avatar asked Jun 21 '11 06:06

cppcoder


People also ask

Can reference point to null?

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.

What happens when you reference a null pointer?

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.

Can we initialize reference to null?

No, references cannot be NULL in C++.

What is attempt to de reference a null object?

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.


2 Answers

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.,

like image 144
Cheers and hth. - Alf Avatar answered Nov 12 '22 16:11

Cheers and hth. - Alf


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

like image 30
Lightness Races in Orbit Avatar answered Nov 12 '22 16:11

Lightness Races in Orbit