Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
Code:
NSLog(@"Long is %i bytes", sizeof(long));
NSLog(@"Unsigned long is %i bytes", sizeof(unsigned long));

gets

Code:
Long is 8 bytes
Unsigned long is 8 bytes
I thought long is 4 bytes and long long is 8 bytes?

(Running 10.6 on 2010 MacBook Pro 13", not sure if that matters.)
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
Code:
NSLog(@"Long is %i bytes", sizeof(long));
NSLog(@"Unsigned long is %i bytes", sizeof(unsigned long));

gets

Code:
Long is 8 bytes
Unsigned long is 8 bytes
I thought long is 4 bytes and long long is 8 bytes?

(Running 10.6 on 2010 MacBook Pro 13", not sure if that matters.)

All a long needs to be is at least the size of an int and equal or less than the size of a long long.

The rest is implementation defined.

For reference an int must be at least 16 bits.
 

ShortCutMan

macrumors member
Aug 8, 2005
41
0
This can have problems with cross platform development. GCC defines long as 8 bytes, whereas Visual Studio's compiler uses 4 bytes for a long and 8 bytes for long long. I do believe that GCC defines long long as 8 bytes as well.
 

foidulus

macrumors 6502a
Jan 15, 2007
904
1
Thats because you are running it on a 64 bit platform and gcc defaults to that architecture when you are compiling.

Here is a little test you can run, enter the following into a file:

#include <stdio.h>
#include <execinfo.h>

int main(int argc, char *argv[]) {

printf("size is %zd\n",sizeof(long));
return 0;
}

now compile it like this:

gcc -arch x86_64 bob.c

run it

now compile the same file with

gcc -arch i386 bob.c

run it again

You will notice that they are different. MS might default to 32 bit in their world which is why you will see it as being 4 bytes

If you want fixed says int values in c use int32_t and int64_t

they will be the same length regardless of platform
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
This is why it is always safer to use the C99 types if you want a specific integer size.

Such as uint8_t and int64_t for instance.

Most of the time there is no need for a specific type. According to all the relevant standards (C, C++, Objective-C, Objective-C++) "long" is a signed type with at least 32 usable bits. So you use long when you need 32 bits, and don't mind getting a few more. And you don't really care about the size (which is 1 one some rather strange implementations of the C language).

There are also the types size_t (big enough to hold the size of any object, or the number of items in any array), intptr_t (big enough to hold the value of any pointer converted to an integer so that you can convert it back without loss), and ptrdiff_t (big enough to hold the difference between any pointers into the same array).
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
It matters immensely. That model is 64-bit capable, and 10.6 defaults to 64-bit when the CPU is 64-bit capable.

Worth adding: Most likely you are building a Universal Binary, possibly PPC + x86 + X86_64. In that case, you have actually three applications in one package, and they can behave differently. For example, an app might display "long = 8 byte" on a 64-bit capable Intel processor, and "long = 4 byte" on a Core Duo (not Core 2 Duo) or PowerPC processor.
 

SidBala

macrumors 6502a
Jun 27, 2010
533
0
Just specify exactly what type you want with the types someone already mentioned.

It's much safer.
 

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
Wow. Many thanks for so many replies. I didn't realize the topic was so complex. But anyway, you guys cleared it up for me. Thanks a ton!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.