C++ definition of NULL

Chad Gatesman chadg@redrose.net
Tue Mar 2 20:37:00 GMT 1999


I am currently porting a very large library that depends on the passing
of NULL into parameters declared as long int.

egcs appears to define NULL as __null which is a builtin symbol that
treats NULL as `{unknown type} *'.  Therefore, I cannot pass in NULL to
a long int parameter without casting it.

e.g.

#include <stdlib.h>

void func(long i)
{
}

void func(int a, int b)
{
}

void main()
{
    func(NULL);
}

> g++ -Wall -c t.cxx
t.cxx:12: warning: return type for `main' changed to `int'
t.cxx: In function `int main(...)':
t.cxx:13: no matching function for call to `func (NULL)'
t.cxx:4: candidates are: func(long int)
t.cxx:8:                 func(int, int)


If this is the case, how can I disable this behavior and use NULL as a
typical 0.  I don't want to have to put a -DNULL=0 in my compile lines,
and I can't  assume that just taking care of it with a redefine in one
of my headers would take care of it, because someone using the library
could end up getting the NULL redefined back to __null.  It would be
nice if the definition in stddef.h was as follows:

#ifndef NULL
#define NULL __null
#endif

Instead of:

#undef
#define NULL __null

Is there a reason it is done this way?  Since it is this why, it
prevents me from redefining it after it is defined the first time and
assuming it will remain that way.

Another thing.  Should this behavior really exist?  The standard states
that NULL should be defined as 0 or 0L.

Suggestions on a good workaround would be nice, but I think this is not
the way NULL should be implemented and it should be reconsidered
defining it as 0.

Thanks,
Chad Gatesman




More information about the Gcc mailing list