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



If you see the ANSI ISO/IEC C++ International Standard(14882) at page 699 it
says:

C.2.2.3 Macro NULL

The macro NULL, defined in any of <clocale>, <cstddef>, <cstdio>, <cstdlib>,
<cstring>, <ctime>, or <cwchar>, is an implementation-defined C++ null
pointer constant in this International Standard(18.1).

And if you see section 18.1:

4. The macro NULL is an implementation-deined C++ null pointer constant in
this International Standard (4.10).180 -> This footnote says: Possible
definitions include 0 and OL, but not (void*)0. - I admit I was wrong about
"(void *)0". However, the stdio.h microsoft version uses it. It seems that
it is left to each compiler (with its .h files) which #define to select.

And 4.10 Pointer Convertion says:

1. A null pointer constant is an integral constant expression (5.19) rvalue
of integer type that evaluates to zero. A null pointer constant can be
converted to a pointer type; the result is the null pointer value of that
type and is distinguishable from every other value of pointer to object or
pointer to function type. Two null pointer values of the same type shall
compare equal. The conversion of a null pointer constant to a pointer to
cv-qualified type is a single conversion, and not the sequence of a pointer
conversion followed by a qualification conversion (4.4).


It is always better to use macros for portability reasons. A single #define
line is easier to change than thousands of 0 or 0L in a complex project. You
can even #undefine and #define again NULL if needed.

Esteban Quijano
Artinsoft corp.

> -----Original Message-----
> From: Paul Davis [mailto:pbd Op Net]
> Sent: Wednesday, February 13, 2002 4:57 PM
> To: Chris Seberino
> Cc: gtk-list gnome org
> Subject: Re: ?? warning: ANSI C++ forbids implicit conversion 
> from `void
> *' in initialization ??? 
> 
> 
> >I looked for NULL explanation about why "= NULL" bad
> >but could not anything.  Can you give me a 1-2 sentence
> >explanation? Is it a security risk somehow or something?
> 
> no, not a security problem. consider how you'd define NULL. 
> 
>     #define NULL ((void *)0)
> 
> 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 *. So in fact, the only acceptable definitions of NULL are
> 0 or 0L or 0UL or 0U, with no type casting. Since the definitions in a
> C header file nearly always include the type cast, and since no type
> cast is acceptable, using NULL is a bad idea. [ quote from a message i
> 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. 
> 
> --p
> _______________________________________________
> gtk-list mailing list
> gtk-list gnome org
> http://mail.gnome.org/mailman/listinfo/gtk-list
> 



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