On Mon, 24 Jan 2011 16:53:48 -0000, Johannes Schlüter
<johan...@schlueters.de> wrote:
On Mon, 2011-01-24 at 13:19 +0100, Pierre Joye wrote:
NULL is for pointers where 0 is for integer-like.
Testing if a ptr is NULL should be done by testing for NULL or not
NULL.
While compilers tolerate *ptr = 0 by casting 0 to NULL, any other
runtime check must use NULL. That's K&R 101.
The standard (quoting ISO/IEC 9899: TC2, but identical in other
versions) states
6.3.2.3 Pointers
3 An integer constant expression with the value 0, or such an
expression cast to type void *, is called a null pointer
constant.55) If a null pointer constant is converted to a
pointer type, the resulting pointer, called a null pointer, is
guaranteed to compare unequal to a pointer to any object or
function.
[...]
6 Any pointer type may be converted to an integer type. Except
as previously specified, the result is implementation-defined.
and
7.17 Common definitions <stddef.h>
3 The macros are
NULL
which expands to an implementation-defined null pointer constant
[...]
So casting (int)0 to a (void*) results to a NULL pointer and is valid.
The other way round is not specified, but implementation defined. By
that Pierre's change is correct. Out of curiosity: I'm not aware of any
implementation (which is in use) where this would be invalid. Anybody
knows one?
Hum? Pierre's change is "correct", but it's also redundant.
Please read the C FAQ: http://c-faq.com/null/ptrtest.html
What would be invalid would be testing for a null pointer with something
like this:
void *p;
...
int a = 0;
if (p == (void*)a) { }
Because "a" is not "a constant integer expression with value 0" (not to
mention, integer to pointer conversions are implementation defined). A
NULL pointer doesn't have to be represented with address 0x0, but a
*constant* 0 is guaranteed to represent a NULL pointer constant.
--
Gustavo Lopes
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php