[Development] 0 vs. NULL

Welbourne Edward edward.welbourne at theqtcompany.com
Thu Oct 8 11:52:14 CEST 2015


> A bit of a generic question, [...] about the preferred use of 0 instead of NULL.

One of the habits of C++ that a C programmer always finds weird.

> Now I remembered having to modify some of my own code to use NULL
> instead of 0 to avoid crashing, on 64bit (capable) hardware. I cannot
> remember the exact details other than that I ended up with undefined
> (= possibly non zero) bits in the more significant 32 bit word, and
> that at least some of the functions involved used va_arg.

That makes sense - in the ... of a var-args parameter-list, the compiler
has no clue what it's passing, so it uses the type of the expression
passed; and it'll read 0 as the int, which is likely 32-bit.  If the
called function is going to be reading it as a pointer, static_cast<void
*>(0) or NULL is necessary to get it passed as a pointer-sized thing;
and I'm not entirely sure about NULL, for that matter (it might just
expand to 0, at least in C, but perhaps not in C++).

> Is it certain that there are no pitfalls that come with trusting the
> compiler to do the proper conversions, on all platforms where Qt is
> supposed to work? Did I simply hit a "feature" Qt won't ever encounter
> because it doesn't use C (nor va_arg)?

Well, it's nice to hear we don't use var-args.  The other pit-fall
that's easy to run into is overloading-by-signature: passing 0 might not
get you the overload that has a pointer as that parameter, unless you
expressly static_cast<>(0) to the type of the intended parameter.

    Eddy.


More information about the Development mailing list