As I pointed out I usually don't have null pointers at all. There might be a few places where a value can very obviously be null, but they are far and far between. No need for documentation.
> Boom segfault.
Which is exactly the right thing to happen (it's the C version of Exceptions in dynamic languages) since the code was incorrect.
> Always check for null. Kind of annoying and lots of people don't for whatever reason.
And now what do you do if you detected null but it was not allowed? Throw an exception? You can have that for free by just not checking.
> Use references. As you say, annoying because then you can't have null (sensibly) even when it would be really useful.
C++ references don't really protect you from NULL. It's just a different syntax for the same thing.
> Use std::optional<int&> or something like that. I only just thought of this and don't know if it would work, but I bet it's a pain.
Yes, pain, big pain. So much line noise and typing (in both senses) around what is essentially an int.
Just don't check. I don't get why there's so many places for null pointers. (As I said elsewhere, I don't have a solution for JSON style microscopic programming, and I don't care about that.).
From the standard:
> 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. ]
#include <stdio.h>
void test(int& x)
{
printf("Hello, world\n"); fflush(stdout);
printf("and the number is: %d\n", x);
}
int main(void)
{
int *x = NULL;
test(*x);
return 0;
}
It's just a syntactic discipline. Null references are undefined in C++, just as NULL dereferences are undefined in C.use assert or some custom always-assert-even-on-release macro.
> Use references. As you say, annoying because then you can't have null (sensibly) even when it would be really useful.
well, only use references for non nullable pointers. Otherwise use plain (or smart) pointers.
> Use std::optional<int&> or something like that. I only just thought of this and don't know if it would work, but I bet it's a pain.
std::optional<T&> is unfortunately not part of C++17, mostly because people couldn't agree on operator= semantics:
int x = 1; int y = 2; std::optional<int&> oint = x;
oint = y; // which one is true? assert(x == y); // deep assign assert(&*oint == &y); // rebind
boost::optional supports references and IIRC rebinds on plain assignment.
There is no guarantee that the pointers are valid, even if not null.
So besides checking for null, to be correct we need to call OS APIs to check for pointer integrity as well.
But I have a feeling that we are lacking a bit of context here. Some people seem to focus on web-application style of programming (understandably) where you have lots of trust issues. Whenever data is carried across trust boundaries it needs to be checked (this applies to integrity in general, of which null safety is just a small part).
(On the other hand, deserialization is not about validation of function arguments. Deserializers should assert integrity on the spot before calling into deeper nested functions).
Making use of ASSERT_VALID(), VERIFY(), AfxCheckMemory(), AfxIsValidAddress(), AfxIsMemoryBlock() and many other helper functions.
A style enforced at the company's code reviews, which helped a lot our code quality.
There are several solutions in C++.
1. Always check for null. Kind of annoying and lots of people don't for whatever reason.
2. Use references. As you say, annoying because then you can't have null (sensibly) even when it would be really useful.
3. Use std::optional<int&> or something like that. I only just thought of this and don't know if it would work, but I bet it's a pain.
So no great solution. Personally I would use a smart pointer type, document it as well as I can, and always check for null.