[chuck-dev] fixed array compatibility checking (PATCH)

Harald hg42 at gmx.net
Thu Sep 6 03:50:10 EDT 2012


>> 1st) I see many places in the ChucK code where null pointers are checked
>> like this:
>> if( pointer )...
>> This is a common mistake since the null pointer does not necessarily
>> have to be 0. That's one of the reasons why there is a NULL macro which
>> may be defined differently (by the operating system or a specific
>> compiler). If I recall correctly it's even a Linux kernel build option.
>> So instead it should read
>> if( pointer != NULL )

http://stackoverflow.com/questions/176989/do-you-use-null-or-0-zero-for-pointers-in-c

from ANSI C Rationale (ANSI X3J11/88-151) (Published 18 November 1988)

"An integral constant expression with the value 0, or such an
expression cast to type void * , is called a null pointer constant.  If
a null pointer constant is assigned to or compared for equality to a
pointer, the constant is converted to a pointer of that type.  Such a
pointer, called a null pointer, is guaranteed to compare unequal to a
pointer to any object or function."

C++98

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2011/n3242.pdf

"4.10 Pointer conversions
1 A null pointer constant is an integral constant expression (5.19)
prvalue of integer type that evaluates to zero
 or a prvalue of type std::nullptr_t. 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. Such a conversion is called a null pointer
conversion."

"4.12 Boolean conversions
A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to
member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member
pointer value is converted to false;
any other value is converted to true. A prvalue of type std::nullptr_t
can be converted to a prvalue of
type bool; the resulting value is false."

so IMHO
- NULL not being a zero value is pre-ANSI C (btw. that's my generation)
- if(pointer)  is C++ and C standard compliant (conversion to bool)
- NULL is not sufficient in many other cases (like overloaded function
selection) and in no way better than 0
  here we have to use (CLASS*)0 instead, with the correct CLASS
- in C++ NULL is usually explicitly defined as 0 whereas in C (#ifndef
__cplusplus) it is defined as (void*)0,
  please look in mixed headers


More information about the chuck-dev mailing list