Re: [Obj-C] if (self) vs. if (self != nil)
Re: [Obj-C] if (self) vs. if (self != nil)
- Subject: Re: [Obj-C] if (self) vs. if (self != nil)
- From: Scott Ribe <email@hidden>
- Date: Fri, 24 Feb 2012 18:53:06 -0700
On Feb 24, 2012, at 1:08 PM, Nick Zitzmann wrote:
>> I also heard that generally speaking NULL is not necessarily always
>> equal to 0 on all architectures.
>>
>> Thoughts?
>
> Where in the world did you hear that? From the C99 standard, 6.3.2.3 paragraph 3:
>
> "An integer 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 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."
>
> I suppose some compilers can ignore the standard and do their own thing, but the compilers that come with Xcode are pretty good with standards compliance.
People just get confused by the point that the "null" or "illegal" pointer value is not all 0 bits on all architectures, but C makes reasonable efforts to treat NULL pointers and 0 as equivalent, regardless of what the underlying value might be, and this equivalence is transparent in most common cases. The word "null" is overloaded, so some people read the warning about the underlying bit representation of a null pointer, and confuse that with the representation at the source code level.
The only places where they're not completely equivalent (in other words not equivalent on weird old architectures) have to do with things like converting/comparing non-constant integers to pointers, where a 0 becomes all 0s regardless of what the null pointer is (which cases are the reason the warning about non-0 null pointers cannot be removed)... Basically, mixing pointers and constant integer 0 expressions to represent NULL works in all expressions on all architectures for all time (ahem, assuming you declare functions new style--if you pass an integer constant to a function that takes a pointer but does not have a declaration visible, on an architecture where where an int is smaller than the pointer, well you deserve what you get for turning off or ignoring compiler warnings in order to use an ancient obsolete style without understanding how to use that style).
--
Scott Ribe
email@hidden
http://www.elevated-dev.com/
(303) 722-0567 voice
_______________________________________________
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