Thread: Using sizeof( long ) at compile time?

  1. #1
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545

    Using sizeof( long ) at compile time?

    Hi,
    I was wondering if there's a way to do something like this in C:
    Code:
    #if sizeof( long ) != 4
    #  error "sizeof( long ) != 4"
    #endif

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My best suggestion would be to use <limits.h>, which contains "LONG_BIT", which should be 32 in your case.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What exactly are you trying to test, the numeric range of a long?
    There are #define constants in limits.h for that.

    Besides, on some machines, sizeof(long) == 1, and it contains 32-bits.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I'm trying to test how many bytes are in a long, int, short... I have a function that will only run on machines where short = 2 bytes, int & long = 4 bytes. If the code is ever compiled on a system where that's not true, I need to generate a compile error so the person porting the code can take the appropriate steps to fix it.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The sort of code that assumes specific sizes for values should really use platform specific typdefs, e.g.
    Code:
    typedef int int32;
    #if LONG_BIT == 64
    typedef long int64;
    #else
    typedef long long int64;
    #endif
    Similarly for unsigned, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by matsp View Post
    My best suggestion would be to use <limits.h>, which contains "LONG_BIT", which should be 32 in your case.

    Mats
    Is LONG_BIT a standard constant? I don't see it in MSVC++ 8.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Maybe I should just do this:
    Code:
    #if UINT_MAX != 0xFFFFFFFF
    #  error "ints aren't 4 bytes here!"
    #endif

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When I looked it up on the web, I got the impression it was standard - but as you say, it's not in the VC <limits.h>.

    You can of course use the constants of LONG_MAX and INT_MAX for example.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    A good optimizing compiler can do it at compile time even if you use code instead of the preprocessor.

    Code:
    if(sizeof(long) == 4)
    {
        do_something();
    }
    else
    {
        do_something_else();
    }
    The sizeof(long) folds to the constant 4, then the constant comparison 4 == 4 folds to the constant 1, then the compiler sees if(1), and optimizes the other case away.

    Of course this doesn't work if you wanted to, say, declare a certain variable only if sizeof(long) == 4.

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by brewbuck View Post
    A good optimizing compiler can do it at compile time even if you use code instead of the preprocessor.
    But that wouldn't issue a compile error, which is what I want.

    Basically, in my code I say the htons()/ntohs()... functions should be used.
    If, for some reason, those functions don't exist on the platform being ported to, they can use my own version of those functions. But if sizeof( short ) != 2 or sizeof( long ) != 4, they need to make some changes to get it to work.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, a runtime check that prints a noticable message and aborts would work.

    Alternatively, you can use a "compile to check" method, so a program that prints the size of relevant types and/or returns a "ok"/"fail" based on that. This is quite often done in automated build scripts.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    But that wouldn't issue a compile error, which is what I want.
    Code:
    if(sizeof(long) != 4)
    {
        some_function_that_doesnt_exist();
    }
    This will create a link failure if sizeof(long) != 4. Again, depends on the compiler optimizing it to oblivion. I don't 100% recommend it, but it's an option.

  13. #13
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Sorry but,
    when you type:
    Code:
    long long var_name;
    or:
    Code:
    long int var_name;
    what doesn't that mean?

  14. #14
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    This is part of the standard integer types (well, at least in the C99 version). This is the complete list
    • char
    • short int
    • int
    • long int
    • long long int

    or, if you prefer,
    • char
    • short
    • int
    • long
    • long long

    Both lists are equivalent.

  15. #15
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Quote Originally Posted by cpjust View Post
    But that wouldn't issue a compile error, which is what I want.
    Looked at the #error preprocessor command?

    Something like

    #if sizeof(long) != 4
    #error Long incorrect size!
    #endif
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Sorting Algorithms with Time
    By silicon in forum C++ Programming
    Replies: 3
    Last Post: 05-03-2005, 11:27 AM
  3. Farmer JoeBot - first project in a LONG time
    By jdinger in forum Game Programming
    Replies: 6
    Last Post: 09-16-2004, 08:57 AM
  4. progarm doesnt compile
    By kashifk in forum Linux Programming
    Replies: 2
    Last Post: 10-25-2003, 05:54 PM
  5. I apologize. Good bye.
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-03-2002, 06:51 PM