4 ms·
> Programmer responsible argued till blue in the face that his code could not have a bug because references (&) cannot be null. Of course they can. It's undefi
by nybble41 5y ago
> Programmer responsible argued till blue in the face that his code could not have a bug because references (&) cannot be null. Of course they can.
It's undefined behavior merely to create a null reference in C++, whether or not it's used. Of course that doesn't mean you can't do it anyway ("int * p = NULL; int &x = * p;") but the bug would be in the code that created the null reference, not the code that used it, just as in Rust the bug would be in the unsafe code which created the null reference—Safe Rust code is allowed to assume that this situation will never occur.
(One side effect of this rule is that the compiler is allowed to assume that "&x == NULL" will always be false for any reference "x", so even if you attempt to check for a null reference the check can be optimized out.)
- lowbloodsugar 5y agoIt was his code the created the null reference. He converted a null pointer into a reference. But doesn't matter: when presented with the evidence of a null reference, it would have been straightforward to diagnose the problem. And after repeated blustering and denials from him, that's what I had to do to get the bug fixed. We do a disservice if we tell people that references (in either language) cannot be null. They can be null, and that would be a bug in some other code - so follow the trail to find where.