Re: !foo vs foo == nil
Re: !foo vs foo == nil
- Subject: Re: !foo vs foo == nil
- From: "Sean McBride" <email@hidden>
- Date: Thu, 21 Aug 2008 14:00:41 -0400
- Organization: Rogue Research
On 8/20/08 11:06 PM, Michael Ash said:
>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.
>
>Interestingly, Apple's vararg methods which use nil as a terminator
>(such as dictionaryWithObjectsAndKeys:) make no mention of this in
>their documentation, and have a great deal of officially sanctioned
>sample code which doesn't use such a cast. (And none of my code uses
>it either.) I suppose Apple must be implicitly making a stronger
>guarantee about the pointer-ness of nil than the C language makes
>about NULL.
gcc provides:
"-Wstrict-null-sentinel (C++ only)
Warn also about the use of an uncasted "NULL" as sentinel. When
compiling only with GCC this is a valid sentinel, as "NULL" is defined
to "__null". Although it is a null pointer constant not a null pointer,
it is guaranteed to of the same size as a pointer. But this use is not
portable across different compilers."
and indeed:
----------
#import <Cocoa/Cocoa.h>
int main(int argc, char *argv[])
{
NSArray* foo = [NSArray arrayWithObjects:@"foo", @"bar", nil];
return 0;
}
----------
$ g++-4.2 -Wformat=2 -Wstrict-null-sentinel /Users/sean/Desktop/test.mm
/Users/sean/Desktop/test.mm:5: warning: missing sentinel in function call
using "(id)nil" in place of "nil" fixes the warning.
--
____________________________________________________________
Sean McBride, B. Eng email@hidden
Rogue Research www.rogue-research.com
Mac Software Developer Montréal, Québec, Canada
_______________________________________________
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