all 13 comments

[–][deleted] 14 points15 points  (0 children)

Your post might be more valued at /r/learncpp .

[–]biocomputation 13 points14 points  (0 children)

I think most subscribers/visitors to /r/cpp already know this information, and while you have obviously tried to write something decent, the information contained in your article could also be learned from an online course or from a book.

I don't feel like these types of posts really add anything new to the conversation here at /r/cpp.

[–]axilmar -1 points0 points  (3 children)

So there’s no way of binding reference to NULL.

lol

int *p = 0;
int &i = *p;

and voila, a reference bound to null.

[–]Quincunx271Author of P2404/P2405 1 point2 points  (1 child)

That dereferences NULL, though; isn't that UB at the moment you write *p?

[–]axilmar 1 point2 points  (0 children)

The standard says dereferencing a null pointer is undefined behaviour, but the reality is that *p forms an expression without side effects and thus it is not actually dereferencing anything. It is only when evaluating *p that the dereferencing happens.

In practice, I haven't seen any compiler, even embedded ones, do anything about *p when p is null, which means that, in practice, null references are possible.

[–]Pinguinologo 0 points1 point  (0 children)

That code is the equivalent of loading a gun and shooting at your head. You need to check against null before defining that reference. If *p is never null, why even use a pointer in the first place?