views:

160

answers:

3

I see NSInteger is used quite often and the typedef for it on the iPhone is a long, so technically I could use it when I am expect int(64) values. But should I be more explicit and use something like int64_t or long directly? What would be the downside of just using long?

+5  A: 

IIRC, long on the iPhone/ARM is 32 bits. If you want a guaranteed 64-bit integer, you should (indeed) use int64_t.

Wevah
Yeah, NSInteger will be the native word type.
Chuck
On x86 `long` is also 32-bit.
KennyTM
@KennyTM: that depends on the OS and compilers, not just the hardware.
Stephen Canon
@Stephan: True. But in this context, the compiler must be gcc or clang, and the OS must be Mac OS X.
KennyTM
@KennyTM: and if the executable is built 64-bit on OS X / x86, `long` is 64 bits wide.
Stephen Canon
But it's not, since the OP is asking about the iPhone. /shrug
Wevah
A: 

If you need a type of known specific size, use the type that has that known specific size: int64_t.

If you need a generic integer type and the size is not important, go ahead and use int or NSInteger.

Stephen Canon
A: 

NSInteger's length depends on whether you are compiling for 32 bit or 64 bit. It's defined as long for 64 bit and iPhone and int for 32 bit.

So on iPhone the length of NSInteger is the same as the length of a long, which is compiler dependent. Most compilers make long the same length as the native word. i.e. 32 bit for 32 bit architectures and 64 bit for 64 bit architectures.

Given the uncertainty over the width of NSInteger, I use it only for types of variables to be used in the Cocoa API when NSInteger is specified. If I need a fixed width type, I go for the ones defined in stdint.h. If I don't care about the width I use the C built in types

JeremyP