Re: What is the default type for an integer literal (as relates to its use in NSLog)?
Re: What is the default type for an integer literal (as relates to its use in NSLog)?
- Subject: Re: What is the default type for an integer literal (as relates to its use in NSLog)?
- From: Stuart Malin <email@hidden>
- Date: Mon, 15 Dec 2008 10:39:47 -1000
On Dec 15, 2008, at 10:00 AM, Nick Zitzmann wrote:
On Dec 15, 2008, at 12:35 PM, Stuart Malin wrote:
I am trying to be 32/64 bit "clean" in some new code that I am
writing. When I declare some integer values, say for named option
values as shown here, what is the type that the compiler assigns to
these values?
enum {
SomeOptionValue = 1,
AnotherOptionValue = 2,
};
Enums are 32-bit constant integers by default. Typedef enums are 32-
bit variable integers. This has not changed in the transition,
mainly because it says in the ANSI C spec that they are the same
size as int. Apple worked around this by making the old typedef
enums in the Tiger SDK into typedef NSIntegers.
Separately, from a space efficiency perspective, would it be better
in the case of having a small set of option values to force the
type to be an unsigned int? That is, if I have a method that takes
such an option, which of the following method signatures is
nowadays preferred?
- (void) someMethodWithOption:(unsigned int)optionValue;
- (void) someMethodWithOption:(NSInteger)optionValue;
Neither. :) You should instead use:
- (void)someMethodWithOption:(NSUInteger)optionValue;
duh! I mixed apples (unsigned) and oranges (integers). My bad.
Note the "U" in NSUInteger means unsigned. But you should not use
unsigned int anymore, because it will truncate 64-bit integers,
which may lead to problems. For example, if you're working with the
return value of -[NSArray indexOfObject:], the value of NSNotFound
changed between 32-bit and 64-bit, and your code needs to be ready
for this.
(Oh, and if you require an argument that is guaranteed to be no
larger than 32 bits in size, then you should use u_int32_t, not
unsigned int.)
I'd suggest turning on the implicit 64-to-32 conversion warning,
then build. That will tell you where there may be trouble.
Thanks for this tip.
I have set GCC_WAR_64_TO_32_BIT_CONVERSION to YES.
Nick Zitzmann
<http://www.chronosnet.com/>
Furthering along the path of getting this right:
If I want to use typedef so that I can enforce the type supplied to a
method, I can declare the option values as:
typedef enum {
SomeOptionValue = 1,
AnotherOptionValue = 2
} MyOptionValue;
and define a method that takes the option as a parameter
- (void) someMethodWithOption:(MyOptionValue)optionVlaue;
Question: Should I force MyOptionValue to be an NSUInteger? If so, how
would I achieve that? (Sorry, my C is weak).
TIA.
_______________________________________________
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