Can references be null?

I often hear people insist that C++ references can be null.

You can easily create a null reference like this:

int *ptr = 0;
int &ref = *ptr;
// ref is a null reference

First, there is no such thing as a null reference in standard C++.

Like all programming languages, C++ is purely a construction of the human mind, and the concepts of the language have been given arbitrary but meaningful names as part of the standardization process so that the users of the language could understand each other. These could be existing words such as class, aligned, statement, pointer or new ones like rvalue or cv-qualified.

In particular, the C++ standard does define a null pointer, but it does not define a null reference because that concept is not part of the language.

This means that instead of having a standard definition (like a null pointer), null references have a commonly accepted definition that follows a similar schema: a null reference is a reference r such that &r is a null pointer. For the rest of this article, I will follow this definition.

Second, a null reference cannot exist in a well-behaved program.

Since the concept does not appear in the standard, it’s pretty obvious that no place in the standard explicitly mentions an operation resulting in a null reference being constructed. In fact, the description of how references are constructed explains that references are constructed from other values, and by definition values cannot have a null pointer for an address.

In particular, the typical construction of a null reference by dereferencing a null pointer is explicitly mentioned in the standard, and described as undefined behavior.

In short, while many modern implementations will let you create a null reference, this will necessarily involve some form of undefined behavior along the way.

I tend to evaluate programmers on a “would I hire this person?” basis. So, what does the “C++ references can be null” statement tell me about someone ?

  • They might be confusing C++ references with C# or Java references, which can be null. Certainly a NO HIRE for a senior C++ programmer.
  • They might have misread their textbook and genuinely think references can be null in the same way as pointers (except, well, the lack of syntactic sugar for checking if a reference is null or creating a null reference strikes me as odd if that were the case). Again, NO HIRE for a senior C++ programmer.
  • They know it’s not standard, but they do not care about writing standard programs. This is an immediate NO HIRE even for junior C++ programmer.

Then, there are those who know about it, and would not use it because they care about writing standard programs. The convention when discussing C++ is to assume that the program is well-behaved, so most people would parse “C++ references can be null” as “C++ references can be null in a well-behaved program”, the latter being obviously incorrect. I don’t really mind a small misunderstanding, the subject being as complex as it is, as long as it’s corrected quickly.

A person who does not is either someone without much experience discussing C++ with others, or a troll. A definite NO HIRE on a team.

7 Responses to “Can references be null?”


  1. 1 Liam

    Whilst I agree with you, if someone was to show me the example code and asked the question I would correct them using some of the explaination you have used.

    The example code is invalid for one reason and one reason only and that is because it dereffences a null pointer.

    Using the term null when talking about references does not make any sense and I would question who or which group of people use the “commonly accepted definition”?
    I would point out to them that a reference can become invalid but this is undefined behaviour by the very nature of references in C++.

  2. 2 Victor Nicollet

    Using the term null when talking about references does make sense (see C#/Java), but the feature is unsupported by C++ and is not defined by the standard. Just like the world “closure” doesn’t stop making sense once you enter the world of C++ :)

  3. 3 Liam

    Ok if you are going to be pedantic when clearly this article is about C++. Using the term null pointer when talking about C++ references makes no sense.

  4. 4 Liam

    LOL -pointer +reference. That is what your arsey pedantic comment did to me.

  5. 5 rip-off

    I like writing standard code, and I would promote the standard argument that references cannot be null in legal code.

    However, for a development position, you generally won’t be dealing with 100% legal C++ code. Very few non-trivial C++ programs manage to avoid skirting through some hairy section of the standard. Even the programmer with the best intentions of following the standard can write code with bugs that cause “null references” to appear.

    I think C++ programmers should be aware of both the theory that references cannot be null, but that in practise you might encounter a null reference.

    So, what does the “C++ references can be null” statement tell me about someone ?

    Either they are like you have described, or possibly that they are seasoned programmers who know that the “impossible” case is all too likely to arise, and that standards tend to be aimed at rather than achieved. Further probing is necessary to distinguish such programmers, rather than writing them off.

    IMHO =)

  6. 6 Victor Nicollet

    I certainly would not advocate judging someone on a single statement in an actual interview ;)

  7. 7 Emmanuel Deloget

    The seasonned programmer is, I hope, knowledgeable enough to understand how to avoid the trap of dereferencing a null/invalid pointer. An invalid reference can be created in two ways :

    1) dereferencing a pointer
    2) using a reference to a variable that went out of scope

    The compiler is supposed to warn you about most occurences of (2) (typical case: when your function returns a reference to an automatic local variable)

    (1) is not supposed to be done at all. I understand that in very rare cases, a programmer might believe that he have to dereference a pointer to initialize a reference, but this is a bad thing and it should be avoided at all cost – mostly because code like this is way too dangerous to end in production code.

Leave a Reply



693 feed subscribers
(readers who polled a feed this week)