Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In either C or C++, should I check pointer parameters against NULL/nullptr?

Tags:

c++

c

null

This question was inspired by this answer.

I've always been of the philosophy that the callee is never responsible when the caller does something stupid, like passing of invalid parameters. I have arrived at this conclusion for several reasons, but perhaps the most important one comes from this article:

Everything not defined is undefined.

If a function doesn't say in it's docs that it's valid to pass nullptr, then you damn well better not be passing nullptr to that function. I don't think it's the responsibility of the callee to deal with such things.

However, I know there are going to be some who disagree with me. I'm curious whether or not I should be checking for these things, and why.

like image 817
Billy ONeal Avatar asked Dec 08 '10 16:12

Billy ONeal


People also ask

Should I use null or nullptr?

As I mentioned above, the general rule of thumb that I recommend is that you should start using nullptr whenever you would have used NULL in the past. As a reminder, since C++11, NULL can be either an integer literal with value zero, or a prvalue of type std::nullptr_t .

Should we set pointers to nullptr?

It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned. This is done at the time of variable declaration. A pointer that is assigned NULL is called a null pointer.

Is nullptr compatible with null?

In C++11 and beyond, a pointer that is ==NULL will also ==nullptr and vice versa. Uses of NULL other than comparing with a pointer (like using it to represent the nul byte at the end of a string) won't work with nullptr .

Is nullptr valid in C?

C. In C, two null pointers of any type are guaranteed to compare equal. The preprocessor macro NULL is defined as an implementation-defined null pointer constant, which in C99 can be portably expressed as ((void *)0) which means that the integer value 0 converted to the type void* (pointer to void).


2 Answers

If you're going to check for NULL pointer arguments where you have not entered into a contract to accept and interpret them, do it with an assert, not a conditional error return. This way the bugs in the caller will be immediately detected and can be fixed, and it makes it easy to disable the overhead in production builds. I question the value of the assert except as documentation however; a segfault from dereferencing the NULL pointer is just as effective for debugging.

If you return an error code to a caller which has already proven itself buggy, the most likely result is that the caller will ignore the error, and bad things will happen much later down the line when the original cause of the error has become difficult or impossible to track down. Why is it reasonable to assume the caller will ignore the error you return? Because the caller already ignored the error return of malloc or fopen or some other library-specific allocation function which returned NULL to indicate an error!

like image 89
R.. GitHub STOP HELPING ICE Avatar answered Oct 25 '22 13:10

R.. GitHub STOP HELPING ICE


In C++, if you don't want to accept NULL pointers, then don't take the chance: accept a reference instead.

like image 38
Karl Knechtel Avatar answered Oct 25 '22 14:10

Karl Knechtel