开发者

What is the preferred way to check for a Null pointer in C++?

Options A:

if (NULL == pSomethingColumn) // Yes, we use Yoda conditions
if (NULL != pSomethingColumn)

Or

if (pSomethingColumn)
if (!pSomethingColumn)

I am looking for references explaining the reasoning as well.

I have heard some people say that technically NULL does not have to be defined as 0, but come on! if that was the case, then the sucker (our app) would crash in -2147483648 different ways.

So, if NULL != 0, then we will have big problems.

Please help me settle a pointless syntax debate. I am not particularly attached to either way; just looking for the official way. Thanks.

P.S. We are using Visual S开发者_如何转开发tudio C++ compiler.


I consider it an accident of history that zero corresponds to false and non-zero corresponds to true; I like to reserve those forms for boolean expressions only. However I don't have a problem with seeing that form in someone else's code.

My vote:

if (pSomethingColumn == NULL)

No, I've never been bitten by forgetting one of the equal signs. Just lucky, or maybe extremely careful.


Neither is clearly better than the other. From a technical standpoint they are equivalent. The number 0 in a pointer context does not mean all bits zero; it is defined to be equivalent to whatever bit pattern is used to represent null pointers. NULL can either be defined as 0 or (void *) 0. Either definition is syntactical and says nothing about the underlying bit representation.

So if (!p) is the same as if (p == NULL) is the same as if (p == 0) as far as technical correctness goes.

The issue is one of style. Like all style issues, your first inclination should be to match whatever the prevailing coding standards are. Do the same thing as the rest of the codebase. If there is no prevailing standard then it comes down to personal preference and you would be encouraged to develop a coding standard with your peers.

(Personally I like to use explicit comparisons and reserve the shorter syntax for boolean values, but I'll gladly use if (!p) that's what everyone else is doing.)


In C++ NULL has to be a null pointer constant in every implementation. 0 as a constant expression is always a null pointer constant (i.e. when converted to a pointer type it yields a null pointer value). Neither of these mean a null pointer value for any given pointer type has to be represented by an address that is zero or all bits zero but this is mostly academic from an application developer's point of view.

To use NULL you should #include a header that defines it such as <cstddef>; you can always use 0.

When converted to a bool a null pointer value always converts to false; any other pointer value always converts to true.

When used as an argument to unary ! a pointer value is converted to a bool as above and then negated.

So long as NULL is defined these are all equivalent:

if (NULL == pSomethingColumn)
if (pSomethingColumn == NULL)
if (0 == pSomethingColumn)
if (pSomethingColumn == 0)
if (!pSomethingColumn)

and these are all equivalent:

if (NULL != pSomethingColumn)
if (pSomethingColumn != NULL)
if (0 != pSomethingColumn)
if (pSomethingColumn != 0)
if (pSomethingColumn)


Call me eccentric, but I actually prefer one from each set. I would use if(ptr) if I'm about to do something that specifically requires the pointer to exist. I would use if(NULL==ptr) if I'm about to do something that specifically requires the pointer to be NULL. I guess this stems from the theory that it's easier to read statements that are not negated.

The more the merrier...if(!(0 != !ptr))


You could probably find links to passionate arguments in favour of either one, but the only answer is: it doesn't matter. Both are correct, and either could be argued to be ever so slightly more expressive than the other, depending on your standards of expressiveness.

Regarding null pointers: while the representation of a null pointer is implementation-dependent, and isn't necessarily a value of zero, a null pointer constant is an integer constant with the value zero. So NULL must be defined as something that is convertible to an integer value of zero - the most obvious definition being 0.


I always prefer

  if (pSomethingColumn != NULL)

Not only because it is principally & conceptually correct, but also because it is self-documenting. Using a particular construct just because "it works" is a sure recipe for non-portability.


NULL is necessarily equal to 0 in any conforming C++ compiler. Personally, I prefer to use NULL because IMHO it's more explicit and the intent is clearer. But Bjarne Stroustrup uses 0 instead of NULL because he prefers to avoid macros. C++0x is going to have an explicit nullptr keyword that will hopefully eliminate this issue once and for all.


I use (!pSomethingColumn) because I'm lazy, but (pSomethingColumn == NULL) is more expressive as it shows the variable is meant to be a pointer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜