Because a null pointer does not point to a meaningful object, an attempt to dereference (i.e., access the data stored at that memory location) a null pointer usually (but not always) causes a run-time error or immediate program crash.
A null pointer constant is an integer constant expression that evaluates to zero. For example, a null pointer constant can be 0, 0L , or such an expression that can be cast to type (void *)0 .
Checking just adds unnecessary clutter to your code, and free(NULL) is guaranteed to be safe. From section 7.20. 3.2/2 of the C99 standard: The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation.
Any pointer type with the value 0 is called a null pointer. Here is the explanation from the C standard §6.3.2.3: An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.
In implementing a menu on an embedded system in C(++) (AVR-Gcc), I ended up with void function pointer that take arguments, and usually make use of them.
// void function prototype
void (*auxFunc)(char *);
In some cases (in fact quite a few), the function actually doesn't need the argument, so I would do something like:
if (something) doAuxFunc(NULL);
I know I could just overload to a different function type, but I'm actually trying not to do this as I am instantiating multiple objects and want to keep them light.
Is calling multiple functions with NULL pointers (when they are intended for an actual pointer) worse than implementing many more function prototypes?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With