Page 1 of 2

NULL not defined?

Posted: Sat Mar 18, 2006 6:41 am
by srg_13
I was adding some code to my OS, and I tried to compile it, but the compiler told me that NULL was not defined. If I try

Code: Select all

int NULL = 0;
then it tells me that I can't compare an integer to a pointer... How can NULL not be defined, and how can I fix it? What can it be defined as?

-Stephen

Re:NULL not defined?

Posted: Sat Mar 18, 2006 6:44 am
by paulbarker

Code: Select all

#include <stddef.h>
This will make sure you have the correct definition of NULL for your compiler and platform.

HTH,
Paul Barker

Re:NULL not defined?

Posted: Sat Mar 18, 2006 6:55 am
by Unlink
Simply,
#ifndef NULL
#define NULL ((void *)0)
#endif

Re:NULL not defined?

Posted: Sat Mar 18, 2006 7:00 am
by srg_13
Thanks.

-Stephen

Re:NULL not defined?

Posted: Sat Mar 18, 2006 8:58 am
by Solar
Sorry, Unlink, but never define something yourself that's defined by the standard. You'll be safe in 99.999% of all cases with your [tt]#define NULL[/tt], and absolutely stumped when the one-in-a-million case pops up where some part of the library depends on NULL being something different...

Re:NULL not defined?

Posted: Sat Mar 18, 2006 10:18 am
by Unlink
i know that solar, i just extract this piece of code from minix, so he knows how it's defined.
he shouldn't depend on this as a default value of NULL while it is part of stddef.h

Re:NULL not defined?

Posted: Sat Mar 18, 2006 1:08 pm
by proxy
it should be noted that while

Code: Select all

#define NULL ((void *)0)
is ok for C it is not 100% correct in c++ because c++ lacks the rule that void pointers are implicitly convertable to any pointer type. If NULL were defined this way in c++ code, there would no legal way to assign NULL to a function pointer since data pointers are not allowed to be converted to function pointers in c++.

So if you intend to define NULL for your c++ code, the best bet is:

Code: Select all

#define NULL 0
since in c++ there is a rule that "an integer expression evaluating to 0 is a null pointer"

proxy

Re:NULL not defined?

Posted: Sat Mar 18, 2006 1:24 pm
by Unlink
Don't forget that i used #ifndef

Re:NULL not defined?

Posted: Sat Mar 18, 2006 1:40 pm
by Solar
Erm... I fail to see how that changes anything? ;)

Re:NULL not defined?

Posted: Sat Mar 18, 2006 8:35 pm
by zloba
are there any reasons to use NULL instead of just 0 ?

what special semantics does NULL provide, and where does it matter?

Re:NULL not defined?

Posted: Sat Mar 18, 2006 8:40 pm
by srg_13
When I tried 0, the compiler complained that I was trying to compare a pointer to an integer.

-Stephen

Re:NULL not defined?

Posted: Sat Mar 18, 2006 11:22 pm
by proxy
it complained when you wrote

Code: Select all

int NULL = 0;
and i assume later

Code: Select all

if(some_pointer == NULL) {}
this is far different than

Code: Select all

if(some_pointer == 0) {}
proxy

Re:NULL not defined?

Posted: Sun Mar 19, 2006 5:27 am
by Solar
zloba wrote: are there any reasons to use NULL instead of just 0 ?

what special semantics does NULL provide, and where does it matter?
The point is that NULL is used throughout the C standard library, and that its actual value is implementation defined (by the library implementation). When a function is advertised to return NULL (like [tt]fopen()[/tt]), it will return whatever the lib defines as NULL, which might be 0 or (void *) 0 or whatever.

That means you cannot mix your own definitions of NULL with those of some existing library. And even if you write your own library, it would be bad style to define NULL in one place and not using that definition consistently yourself.

Re:NULL not defined?

Posted: Mon Mar 20, 2006 3:48 am
by RetainSoftware
skip the NULL comparison all together ;D

if(NULL == pointer)

is the same as

if(!pointer)

and is just as readable and works in c/c++

greets,

Rene

Re:NULL not defined?

Posted: Mon Mar 20, 2006 3:57 am
by RetainSoftware
just on a side note:

when comparing it's better to put the constant on the left side of the comparison instead of the right side. for instance

use

Code: Select all

if(NULL == pointer)
instead of

Code: Select all

if(pointer == NULL)
the reason is explained by typing = instead of ==.

Code: Select all

if(pointer = NULL)
will assign NULL to pointer an will always return false and

Code: Select all

if(NULL = pointer)
will generate an compile time error and safe you from a lot of debugging sessions

just my 2 cents