[omniORB] [omnithread] Why is NULL defined as (void*)0?

Keeley, Michael mkeeley@orchestream.com
Fri, 24 Aug 2001 09:55:40 +0100


Hmm,

I don't agree with your definition of the convention of using NULL.
NULL is supposed to refer to a pointer that does not refer to an object;
which does not preclude it from being used in assignment or return
statements.

In Stroustrop's C++ (3rd ed) p5.1.1 he even recommends defining NULL as

const int NULL = 0;

He also says that "Because of C++'s tighter type-checking, the use of a
plain 0, rather than any suggested NULL macro, leads to fewer problems."

MikeK.

-----Original Message-----

> 
> In the omnithread.h header, NULL is defined to be (void*)0.
> 
> Shouldn't this be just 0?
> Otherwise code such as
> 
> #define NULL (void*) 0
> 
> char* foo() {
>   return NULL;
> }

I believe you are a bit confused about the convention for
using NULL.  It's used in making comparisons with pointers
to objects of unknown type.  E.g.:

if ( foo() == NULL ) { ... };

(The void* gets caste to a char* for the comparison.)

No compiler will allow you to return a different "type"
than you have declared it to return.  If your function wanted
to return a long*, you would still have to caste it to
a char* before returning it.

Richard

There's altogether too much blood in my caffeine system. 8^)

> 
> will not compile - void* cannot be cast to char*.
> 
> Mike.
> 
> Michael Keeley


--
This communication contains confidential information intended solely for the use of the individual/s and/or entity or entities to whom it was intended to be addressed.  If you are not the intended recipient, be aware that any disclosure, copying, distribution, or use of the contents of this transmission is prohibited.  If you have received this communication in error, please contact the sender immediately, delete this communication from your system, and do not disclose its contents to any third party, or use its contents.  Any opinions expressed are solely those of the author and do not necessarily represent those of Orchestream Ltd or its group of companies unless otherwise specifically stated.