Re: Re: svn: /php/php-src/ branches/PHP_5_3/ext/date/php_date.c trunk/ext/date/php_date.c

From: Date: Mon, 24 Jan 2011 19:11:35 +0000
Subject: Re: Re: svn: /php/php-src/ branches/PHP_5_3/ext/date/php_date.c trunk/ext/date/php_date.c
References: 1 2 3 4 5 6  Groups: php.internals 
Request: Send a blank email to internals+get-51346@lists.php.net to get a copy of this message
On Mon, 24 Jan 2011 16:53:48 -0000, Johannes Schlüter <johannes@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

« previous php.internals (#51346) next »