sizeof(long) gets me 8 bytes? not 4?

Discussion in 'Mac Programming' started by zippyfly, Sep 23, 2010.

  1. macrumors regular

    Joined:
    Mar 22, 2008
    #1
    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.)
     
  2. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #2
    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.
     
  3. macrumors member

    Joined:
    Aug 8, 2005
    #3
    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.
     
  4. macrumors 6502a

    Joined:
    Jan 15, 2007
    #4
    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:

    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
     
  5. macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #5
    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.
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    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).
     
  7. macrumors 603

    Joined:
    Aug 9, 2009
    #7
     
  8. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #8
    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.
     
  9. macrumors 6502a

    Joined:
    Jun 27, 2010
    #9
    Just specify exactly what type you want with the types someone already mentioned.

    It's much safer.
     
  10. thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #10
    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!
     
  11. macrumors 68000

    Joined:
    Mar 29, 2009
    Location:
    USA
    #11

Share This Page