[LLVMdev] [cfe-dev] Style question: NULL or 0?

Chris Lattner clattner at apple.com
Wed Aug 17 23:38:12 PDT 2011


On Aug 17, 2011, at 9:06 PM, Marshall Clow wrote:

> On Aug 17, 2011, at 4:41 PM, Jordy Rose wrote:
> 
>> Hi, LLVM. I have a question I'd like to get put into the official style guidelines: do we prefer NULL or 0 for C++ objects? I've seen both throughout the code.
>> 
>> Personally I prefer NULL, since it establishes that something is a pointer and not an integer (or integer-constructed object, but thankfully we avoid implicit conversions in LLVM/Clang). But I think I read somewhere that 0 is more C++esque. (And C++11 nullptr's not available yet, of course.)
>> 
>> 
>> Similarly, when testing for the null pointer, is it better to use:
>> 1. (x) and (!x)
>> 2. (x) and (x == NULL)  // or 0
>> 3. (x != NULL) and (x == NULL)
>> 4. (NULL != x) and (NULL == x)
>> 5. ...some other combination or something I haven't thought of
>> 
>> I don't care about this one as much. Still, in this case I think the first is fairly standard, though the second does make the explicit test for NULL stand out a little more (fewer copy/paste errors, perhaps?). IMO (3) and (4) are overkill.
> 
> 
> I use #4 most of the time.
> Rationale:
> * Make it explicit
> * Constant on the left hand side.

Please don't do this in LLVM code.  Unless you're the terminator of a varargs call, I'd prefer to use 0 instead of NULL, and definitely constants on right hand sides of comparisons.

-Chris



More information about the llvm-dev mailing list