If using pointers, always verify if they are NULL before using them.
Note that you do not need to check for NULL before calling free. For instance, the following is perfectly valid. int *x = NULL; free(x): If you're interested in a difference between C and C++, check out this tip on memory allocation in C++.
A null pointer has a reserved value that is called a null pointer constant for indicating that the pointer does not point to any valid object or function. You can use null pointers in the following cases: Initialize pointers. Represent conditions such as the end of a list of unknown length.
In C or C++, there is no special method for comparing NULL values. We can use if statements to check whether a variable is null or not.
This seems like a lot of work; to check for null each time an object is used.
I have been advised that it is a good idea to check for null pointers so you don't have to spend time looking for where segmentation faults occur.
Just wondering what the community here thinks?
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