Re: using literal zero for NULL



Jon A. Cruz wrote:

Thanks. I am using c++ so I must use protptypes. I use -Wall also.


For C++, 0 is supposed to be preferred over NULL. For varargs, though, the compiler might not know your intent. I've seen places that state modern compliers treat NULL as exactly 0 (in which case static_cast<void*>(0) should do the trick), however you should probably check on a C++ newsgroup for details.

I did the following program in C++:

#include <stdio.h>
int main(int argc, char** argv)
{
        printf("NULL: %d\n", sizeof(NULL));
        printf("zero: %d\n", sizeof(0));
}

The output on amd64 is as follow:

NULL: 8
zero: 4

Note that in C, I get the exact same result.

So there is no reason to prefer 0 over NULL for pointers.

Hub



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]