In the following code, what is the benefit of using (!!p)
instead of (p != NULL)
?
AClass *p = getInstanceOfAClass();
if( !!p )
// do something
else
// do something without having valid pointer
Setting pointers to NULL or to another valid value after memory is freed is a simple and easily implemented solution for reducing dangling pointers. Dangling pointers can result in freeing memory multiple times or in writing to memory that has already been freed.
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.
Does 'free' make a pointer null? No, because it can't. For example: void *ptr = malloc(number_of_bytes);
null means that there is no value found. and 0 means zero but zero is still a value which is zero.
It is pretty much the same, although I consider the !!p
to be bad style, and usually indicates a coder trying to be clever.
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