Re: ?? warning: ANSI C++ forbids implicit conversion from `void *' in initialization ???



On Wed, 13 Feb 2002 17:57:11 -0500
Paul Davis <pbd Op Net> wrote:

> no, not a security problem. consider how you'd define NULL. 
> 
>     #define NULL ((void *)0)

Although this form is allowed, it is *only* valid for C (not C++), and is
not how most compilers define NULL.

> now try to use this:
> 
>     SomeObject *ptr = NULL;
> 
> because C++ is much stricter about types, the compiler will/may
> complain that it cannot perform an implicit cast of void * to
> SomeObject *.

Correct.  It's because of this (questionable) NULL #define in the GTK code
that C++ developers have problems using GTK...
(I've tried to point this out before, but it doesn't seem like anyone
wants to fix the problem... (sigh))

> So in fact, the only acceptable definitions of NULL are
> 0 or 0L or 0UL or 0U, with no type casting.

Correct.  This is how every compiler I've seen defines it.
[ Granted, I only have access to 6 distinct compilers, from 6 different
vendors.... :-/ ]

> Since the definitions in a
> C header file nearly always include the type cast, and since no type
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This may have been the case years ago, but any compiler doing that now
is pretty rare...

> cast is acceptable, using NULL is a bad idea. [ quote from a message 
> found with google]:
> 
>    the C++ standard guarantees that if you use 0 in a pointer context,
>    it will be converted to whatever that platform uses for the "null
>    pointer", even if it happens not to have a bit pattern of all
>    zeroes.  (Section 4.10) Therefore, it's safe to use 0 to mean "the
>    null pointer".
> 
> so, just stick to 0. 

Using NULL is perfectably acceptable, as long as NULL is correctly defined.
It is a notational form that adds clarity to the intent of the code (and is
easy to search for!).  There is absolutely nothing wrong with using NULL in
code.
Please review the comp.lang.c FAQ, section 5
http://www.eskimo.com/~scs/C-faq/s5.html

Summary:
GTK has an poor/incorrect definition of NULL that breaks C++ code.  At a
minimum, the definition should be fixed.  However, the better fix would be
to simply remove the definition, and use the one provided by the compiler.

Ian



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