Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: "Clark Cox" <email@hidden>
- Date: Wed, 20 Aug 2008 20:36:10 -0700
On Wed, Aug 20, 2008 at 8:31 PM, Andrew Farmer <email@hidden> wrote:
> On 20 Aug 08, at 20:06, Michael Ash wrote:
>>
>> On Wed, Aug 20, 2008 at 8:34 PM, Douglas Davidson <email@hidden>
>> wrote:
>>>
>>> Well, after all, zero is zero, how much difference can it make? Quite a
>>> bit, as it turns out, since in 64-bit one of them is four bytes of zero,
>>> and
>>> the other is eight bytes of zero. If you're just comparing against NULL,
>>> it
>>> doesn't matter, but if you're using it in something where size
>>> counts--say,
>>> a list of vararg arguments--then it matters a lot. It's not easy to
>>> debug,
>>> though, because who would think that you need to distinguish one NULL
>>> from
>>> another?
>>
>> It is a little known fact that when passing NULL (and by extension nil
>> or Nil) as a parameter to a vararg function, you *must* cast it to the
>> appropriate pointer type to guarantee correct behavior.
>
> Source (and, preferably, example) please? A pointer is a pointer is a
> pointer; the internal representation of (char *) NULL is identical to (void
> *) NULL or (NSRect *) NULL or (id) nil or what-have-you.
The C standard doesn't require that NULL has a pointer type. The
following is a perfectly valid definition of the NULL macro:
#define NULL 0
If you pass that to a vararg function, it will be passed as if it were an int.
>From the C standard:
"An integer constant expression with the value 0, or such an
expression cast to type
void *, is called a null pointer constant."
--
Clark S. Cox III
email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden