Monthly Archive for November, 2009

Happy Birthday, Pizza!

A year ago, I ordered a €9 pizza from Speed Rabbit Pizza, a Paris-based pizza delivery chain. A meal was had by me. Verily.

pizza

The next evening, I received a text message on my cell phone about a promotional 3-for-2 offer from that very Speed Rabbit Pizza outlet. I had made the mistake of giving them my cell phone number, because they wanted to call me if the pizza delivery man got stuck in a door. I have been receiving such weekly reminder messages every week for an entire year . And since they also had my home address, they could even send me promotional envelopes by mail.

I am quite protective of my personal contact information, and actively avoid doing business with any company that contacts me without my permission, so I have taken my business to Pizza Hut instead.

Total money made by Speed Rabbit pizza from me: €8.53 (without tax)

Total money spent by Speed Rabbit pizza on sending me promotional messages: 52 × €0.07 (text messages) + 5 × €0.7 (postal mail) = €7.14 (without tax)

Total money earned by Pizza Hut because Speed Rabbit sent me promotional messages: around €105 (without tax)

Making your blog post look like a Mastercard ad: priceless.

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.



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