Re: Checking for NULL (was "Re: Can't get setDelegate to work...")
Re: Checking for NULL (was "Re: Can't get setDelegate to work...")
- Subject: Re: Checking for NULL (was "Re: Can't get setDelegate to work...")
- From: Clark Cox <email@hidden>
- Date: Wed, 4 Mar 2009 15:12:50 -0800
On Wed, Mar 4, 2009 at 2:40 PM, Stuart Malin <email@hidden> wrote:
>
> On Mar 4, 2009, at 12:15 PM, email@hidden wrote:
>
>> In any case, and perhaps based purely on habit, I find it difficult to
>> even write:
>>
>> NSStatusItem statusItem = [[[NSStatusBar systemStatusBar]
>> statusItemWithLength:NSVariableStatusItemLength] retain];
>>
>> ...in favor of:
>>
>> NSStatusBar *systemBar = [NSStatusBar systemStatusBar];
>> if (systemBar != NULL)
>> {
>
> AFAIU(nderstand):
>
> While nil and NULL may operate interchangeably (in most cases?),
> semantically, they are not the same thing.
>
> In your code fragment above, I believe most Cocoa programmers would expect
> to see:
>
> if (systemBar != nil) ....
>
> NULL is used more to indicate a null value c pointer.
And all object pointers are c pointers, so NULL is still appropriate.
There is no situation where nil can be used that NULL (or even just a
literal zero) cannot.
> At one time I was under the impression they were defined differently:
> nil being of type id, and NULL being a void*
The null pointer constant, in C, can either be a compile-time integer
constant with the value zero, or such a constant cast to (void*). (I'm
paraphrasing), so all of the following are valid definitions of the
NULL macro:
#define NULL 0
#define NULL (42-42)
#define NULL !1
#define NULL (void*)0
or even:
#define NULL __some_special_token_that_is_specific_to_your_compiler
etc.
> But in trying to research this, I run across much conflicting info and
> suspect perhaps this has either changed over time, and/or depends on whether
> the use is in/with C++ or not.
In C++, on the other hand, it must *not* be cast to (void*). Because
of the need for compatibility with both C and C++ in their headers,
most implementations either define it as a simple, literal zero, or
use some compiler built-in.
> Perhaps some of the experts on this list can
> provide some clarity....
--
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