difference between if(pointer) vs if(pointer != NULL) in c++, cpplint issue
Asked Answered
C

6

12

I already checked this post Can I use if (pointer) instead of if (pointer != NULL)? and some other posts on net.

But it is not stating any difference between two statements.

Problem: As I run cpplint.py on my cpp code, I found issues where I check pointers for NULL. I preferred to check using simple

if(pointer)         //statement1

but cpplint says you should check like

if(pointer != NULL)        //statement2

So I just want to know , Are there any benefits of statement2 over statement1 ? Are there some scenarios in which statement1 may create problem ?

Working: As per my knowledge there is no difference in working of both statements. Its just a change of coding style.

I prefer to use like statement1, because

  • Its Simple, Readable
  • No Tension of missing (=) by mistake over equality(==) in a comparison

But cpplint is raising this as issue, then there might be some benefit that I missed.

Note: Java also doesn't support statement1.

Centare answered 31/7, 2016 at 10:0 Comment(5)
Why tag [C] if question is "difference ...in c++ ..."? ... and then mention [Java]?Cranford
I added [C] because this question is also valid in C. I mentioned java, because I just wanted to provided information that How other languages handle this scenario.Centare
The question may be valid in many other languages such as C, but the answer details are different. Better answers come with specifying a single target language.Cranford
Yes you are right. Question s should be asked to target single language only. But in this particular case ansers details to both c & c++ are same. Is not it ? Thats why i tagged C too.Centare
No, they answers can differ. The details and nuances of NULL and if (ptr) are subtle enough in one language, let alone two.Cranford
T
6

No, if pointer is really a pointer type there is no difference, so everything here is a question of coding style. Coding style in turn depends on habits in different communities so there can't be a general recommendation.

I personally prefer the first because it is shorter and more to the point and avoids the use of the bogus macro NULL.

In C NULL can be very different things (integer or pointer) and in C++ its use is even deprecated nowadays. You should at least use nullptr, there.

Tanta answered 31/7, 2016 at 10:5 Comment(2)
It can be very different things (int and pointer), but cannot be very different other things (std::optional<>, empty container / string, etc.). We're sort of missing a nothing_t nothing conceptually, sometimes I feel.Piccalilli
@lorro, I was clearly talking about C in that phrase.Tanta
P
3

You are using Hungarian notation, where it's possible to tell if a variable is a pointer. As long as it is - either native or smart - there's no difference. However, when someone changes it to another indirect type (e.g., std::optional<>), then the second will fail. So my suggestion is to keep on using the first: it's not Java, it's C++.

Piccalilli answered 31/7, 2016 at 10:7 Comment(2)
That code is not using Hungarian notation. Hungarian notation uses prefixes to indicate types.Cryoscopy
Ok, the idea is that you can tell the type from variable name.Piccalilli
S
3

In C++, assuming ptr is a pointer, the comparisons if (ptr) and if (ptr != NULL) are functionally equivalent.

In C++11 and later, it is often considered preferable to use the alternative if (ptr != nullptr).

For a simple check of a pointer, the differences in these options are really stylistic. The mechanisms might differ slightly, but the end result is the same.

cpplint, like most automated checkers, tends to - by default - complain more about breaches of some style guidelines more than others. Whether any particular set of guidelines is right or wrong depends on what is needed for your project.

For class types that can sensibly be compared with a pointer (e.g. smart pointer types) the preferred test depends on what set of operations (comparison operators, implicit conversions, etc) that type supports.

Seizing answered 31/7, 2016 at 13:43 Comment(0)
N
1

In C, onsider :

int *ptr=malloc(10*sizeof *ptr);
free(ptr); // though the memory is freed, the ptr is not auto-set to NULL
if (ptr)
{
printf ("ptr is not null\n");
}

So you are expected to put

ptr=NULL; // ptr is explicitly made to point at nothing
// The above step is mandatory.

after the free.

So as a response in the the if-statement, one might recommend to do

if ( ptr == NULL ) // This is mostly a coding style & improves readability?

or better

if ( NULL == ptr ) // less chances of error

Well, the [ site ] says about cpplintthat it is :

An automated checker to make sure a C++ file follows Google's C++ style guide

So again, it is somebody's style that matters. Say , if you contribute to somebody's code in google, they expect you to follow this style where it facilitates easy collaboration.

Neume answered 31/7, 2016 at 10:17 Comment(3)
Yes, Why google is following second statement over first, that is the question actuallyCentare
Why cannot be answered imho, i just wanted to say, if you're contributing to some google-code projects, you need to(ie 'must') follow this style for your code to be accepted.Neume
@Centare : Also, i follow this style in (some) case/s as mentioned in my example where it is easier to understand what is going on, if(ptr != NULL) makes me literally say is the pointer not equal to null?Neume
T
1

There is one scenario that may create a problem using statement1.

Consider the following code which could have two different meanings.

bool* is_done = ...;

// Is this checking if `is_done` is not null, or actually
// intended to check if `*is_done` is true?
if (is_done) {
  ...
}

If you intended to do a null check, you're fine. But if your original intent is to check if *is_done is true but missed an asterisk by accident, this code may result in a totally unwanted behavior and require you to spend X hours to figure out the culprit.

This could've avoided by explicitly checking the statement like

// Now this results in a compile error and forces you to write
// `*is_done` instead.
if (is_done == true) {
  ...
}

This is applicable to any types that could be implicitly converted to bool like std::unique_ptr.

Someone may argue that the above case is too rare and still prefer the statement1 in favor of simplicity. I think it is fair and both styles are acceptable. But some organizations, like Google, may encourage you to follow their coding style to keep the lesson they previously learned.

Teenager answered 27/3, 2020 at 17:36 Comment(0)
S
-1

There is no difference between both if(pointer) and if(pointer != NULL). if(pointer) is used for the code optimization.

Slr answered 31/7, 2016 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.