Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: Negm-Awad Amin <email@hidden>
- Date: Thu, 21 Aug 2008 11:04:21 +0200
Am Do,21.08.2008 um 10:47 schrieb Thomas Engelmeier:
Am 21.08.2008 um 05:03 schrieb Michael Ash:
There was a common perception that NULL is not really the same as
nil. But
seems like in the end it really is (void*)0.
They differ in type, not in value.
"NULL" is (void *) 0.
"nil" is (id) 0.
"Nil" is (Class) 0.
This is true conceptually but not as far as their actual definition.
NULL can be either 0 or (void *)0.
Let's be a little bit more precise, I'll try to paraphrase correctly
from my memory:
a.)
(int) NULL is NOT required or guaranteed 0x0 by the standard. This
is why one should never use
if( anPtr == (void *) 0 );
instead of
if( anPtr == NULL );
On modern machines, it usually is.
b.) At least the C++ standard mandates for type conversion of an
pointer to an boolean, that e.g. ( !anPtr ) is equivalent to ( !
(anPtr == NULL )). I didn't read the C99 spec but I'm sure there is
something similar specified.
This implementation detail does probably not matter on most
platforms today as even microcontrollers have AFAIK linear address
spaces.
When I was writing my first C app on a DOS machine, there were
segmented addresses. That meant it could happen that
((somePtr + offset) - offset) == somePtr
evaluated to false - (somePtr + offset) changed the segment,
anotherPtr - offset did not. Big debug surprise for poor little
68020 fanboy ;-)
First: Thanks a lot! I wanted to write somthing like this, but …
I always say, that theoretically it is dangerous to relay on NO = 0
and nil = 0 (The assumption YES = 1 is simply wrong on some machines).
In practice of course this always works.
But in the documentation of Objective-C 2 from Apple, the definitions
of NO and nil are diclosed. So I think, now it is for (and only for)
Objective-C 2 "official" that NO and nil equals to 0.
Sometimes you see in source code something like this:
if( booleanVarOrExpression == YES )
(I think, Rentzsch does it that way, IIRC) and Ithink, that this is
simply correct.
Cheers,
Amin
Regards,
Tom_E
_______________________________________________
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
Amin Negm-Awad
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