hiddensunset4
hiddensunset4

Reputation: 6029

Null References - Where in the C++ Standard

Is there any section in the C++ standard the shows that NULL references are ill-formed? I am trying to show my lecturer (this is for an assignment for which I am being graded) that the following expression is undefined behaviour:

AClass* ptr = 0;
AClass& ali = *ptr;
std::cout << "\n" << (AClass*) &ali << '\n';

The violations I see, is dereferencing of a null pointer, and then referencing a null reference. In an a program he is using as a correct example, he is comparing the return of the dereferenced pointer reference:

(AClass*) &ali != (AClass*) 0

As a test for an objects validity. I saw this as completely undefined behavior; I want to find a quote from the standard that is a bit more concrete for my explanation.

If I'm wrong, then please show where I have made an error.

Upvotes: 2

Views: 368

Answers (2)

kiriloff
kiriloff

Reputation: 26333

You should use pointers, and not references, if you wish to reassign. References are initialized once and for all and cannot be reassigned. Pointers can be created but left uninitialized, plus they can be reassigned.

8.3.2/1:

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. As described in 9.6, a reference cannot be bound directly to a bit-field]

1.9/4:

Certain other operations are described in this International Standard as undefined 
(for example, the effect of dereferencing the null pointer)

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490178

§8.5.3/1: "A variable declared to be a T&, that is “reference to type T” (8.3.2), shall be initialized by an object, or function, of type T or by an object that can be converted into a T."

The code above does not initialize the reference with an object or function of type T or an object that can be converted to T. The violates the "shall". At that point, the only room for question is whether it's undefined behavior, or whether this qualifies as a diagnosable rule, in which case the compiler would be required to give an error message. Either way, it's clearly wrong though.

Upvotes: 6

Related Questions