Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why/When to use (!!p) instead of (p != NULL)

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
like image 429
Juan Macek Avatar asked Oct 22 '09 08:10

Juan Macek


People also ask

Why is it important to set a pointer to NULL after passing it as an argument to free ()?

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.

What does it mean to set a pointer to NULL?

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 set ptr to NULL?

Does 'free' make a pointer null? No, because it can't. For example: void *ptr = malloc(number_of_bytes);

Is zero the same as NULL C++?

null means that there is no value found. and 0 means zero but zero is still a value which is zero.


1 Answers

It is pretty much the same, although I consider the !!p to be bad style, and usually indicates a coder trying to be clever.

like image 176
Wernsey Avatar answered Sep 22 '22 11:09

Wernsey