NULL not defined?

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
srg_13

NULL not defined?

Post 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
paulbarker

Re:NULL not defined?

Post 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
Unlink

Re:NULL not defined?

Post by Unlink »

Simply,
#ifndef NULL
#define NULL ((void *)0)
#endif
srg_13

Re:NULL not defined?

Post by srg_13 »

Thanks.

-Stephen
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:NULL not defined?

Post 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...
Every good solution is obvious once you've found it.
Unlink

Re:NULL not defined?

Post 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
proxy

Re:NULL not defined?

Post 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
Unlink

Re:NULL not defined?

Post by Unlink »

Don't forget that i used #ifndef
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:NULL not defined?

Post by Solar »

Erm... I fail to see how that changes anything? ;)
Every good solution is obvious once you've found it.
zloba

Re:NULL not defined?

Post by zloba »

are there any reasons to use NULL instead of just 0 ?

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

Re:NULL not defined?

Post by srg_13 »

When I tried 0, the compiler complained that I was trying to compare a pointer to an integer.

-Stephen
proxy

Re:NULL not defined?

Post 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
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:NULL not defined?

Post 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.
Every good solution is obvious once you've found it.
RetainSoftware

Re:NULL not defined?

Post 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
RetainSoftware

Re:NULL not defined?

Post 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
Post Reply