Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: "Clark Cox" <email@hidden>
- Date: Thu, 21 Aug 2008 15:36:42 -0700
On Thu, Aug 21, 2008 at 3:19 PM, Scott Ribe <email@hidden> wrote:
>> Please show me where that is guaranteed.
>
> Well, you've really sent me on an archeological dig. It's not in the
> standard! I think there's an error of omission--yeah I know, claiming "I'm
> not wrong, the standard is" is quite the claim indeed, but bear with me a
> second:
>
> 6.3.2.3.5 covers integer -> pointer, and says "Except as previously
> specified..." where that clearly refers to 6.3.2.3.3 converting integer
> constant 0 to null pointer.
Yes, but *only* a constant 0. The following is not guaranteed to set
ptr to null:
int i = 0;
void *p = (void*)i;
> As I pointed out before, 6.3.2.3.6 covering pointer -> integer says "Except
> as previously specified..." I assumed that referred to the requirement that
> null pointer -> integer must yield 0, but a thorough search of the document
> reveals that *nothing* was previously specified. Clearly, an error in the
> standard--either of omission of whatever it was that was supposed to be
> previously specified, or of editing allowing that "dangling reference".
It is an intentional omission (I have had this argument before on
comp.lang.c, except I was arguing your side at the time, and was set
straight). For example, platforms with segmented memory models can
have multiple different representations of null pointers, converting
these back to int don't necessarily yield 0. This is allowed by the
standard.
Note, I'm not claiming that you can't convert a pointer to an integer,
just that the mapping from pointer to an integer (of sufficient size)
is implementation defined, and is not guaranteed to map null to zero.
The only guarantee you have is that if you later convert that integer
back to a pointer, it will compare equal to the original pointer.
Of course, this is, for the most part, all academic, as any platforms
on which Cocoa (or OpenStep APIs) exist all convert a null pointer to
zero and vice-versa.
--
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