Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there no "NULL reference" in C++?

I was reading the C++ FAQ - "8.6 - When should I use references, and when should I use pointers?" and in particular this statement:

Use references when you can, and pointers when you have to.

...

The exception to the above is where a function's parameter or return value needs a "sentinel" reference — a reference that does not refer to an object. This is usually best done by returning/taking a pointer, and giving the NULL pointer this special significance (references must always alias objects, not a dereferenced NULL pointer).

From what I've seen, the need for a "sentinel" reference is indeed often the reason to use pointers instead of references. What I'm wondering is: why doesn't C++ have a special "NULL value" for references? It seems it would make pointers almost unnecessary, which would solve many problems.

So why wasn't it part of the language specification?

Edit:

I'm not sure my question is clear - I guess I'm not asking litterally about NULL references. Most often I read that in C++ "the reference is the object". And, in most OOP languages, objects can be NULL - Pascal, C#, Java, JavaScript, PHP, etc. in all these you can do someObject = null or someObject := nil. In fact, Pascal also supports pointers but still allows objects to be nil, since it has its use. So why is C++ somehow special and doesn't have a NULL object? Was it just an overlook or an actual decision?

like image 853
laurent Avatar asked Jul 10 '12 07:07

laurent


1 Answers

Because a reference carries the semantic that it points to a valid memory address that never changes; i.e. that dereferencing it is safe/defined and so no NULL checks are required. References cannot be reassigned by design.

You use a pointer when the var can be NULL and client code has to handle that case. You use a reference when you can guarantee a valid/initialised memory address.

One example of using pointers is as a member of a class to store a "reference" to some instance that might not be known or able to be initialised at class construction time. However, member references must be initialised at construction time (via initialiser lists) and their assignment cannot be deferred.

If you allow a null reference it is then no different to a pointer besides syntax (the same NULL checks would need to take place.)

Update:

"And, in most OOP languages, objects can be NULL - Pascal, C#, Java, JavaScript, PHP, etc. [...] So why is C++ somehow special and doesn't have a NULL object? Was it just an overlook or an actual decision?"

I think you are a bit confused about this. Java and C# etc. might give the impression of "NULL objects", but these object references are more or less like a C++ pointer with simpler syntax, GC instrumentation and exception throwing. In those languages if you operate on a "Null Object" you will get some kind of exception like NullReferenceException (C#). Hell, in Java its called a NullPointerException.

You have to check for null before you can use them safely. Kind of like C++ pointers (except in most managed languages, pointers are initialised to NULL by default, whereas in C++ its usually up to you to take care of setting the initial pointer value (otherwise undefined/whatever memory was already there)).

The C++ view is about having choice, and hence being verbose:

  • Use a plain pointer to do how you please, checking NULL where necessary.
  • Use references which have a compiler-enforced validity semantic/constraint.
  • Roll your own smart pointers that do bookkeeping and behave whichever way you want them to.
  • Using void pointers (cautiously!) to reference an untyped block of memory if ever required.
like image 176
Preet Kukreti Avatar answered Oct 20 '22 04:10

Preet Kukreti