CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2009
    Posts
    2

    Lightbulb integer type, still machine dependent?

    Hello everyone,

    I hope my question is not so silly, but when I started to learn C-language 10 years ago, books was mentioning that the data type "int" is machine dependent, and this was when there was a change from 16 to 32 bit machines. Now we are in the move between 32 and 64 bit, is "int" still machine dependent? If yes, why does it exist if we can rely on short and long which have a fixed size?

    Many thanks in advance.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: integer type, still machine dependent?

    All three of those types have machine-dependent size. The only guarantees I know of in the standard are 1) char can at least handle a numeric range equivalent to 8 bits, 2) long can handle at least a numeric range equivalent to 32 bits, and 3) sizeof(char) <= sizeof(short) <= sizeof(int) <= sizeof(long).

    If you need your type to have a specific size, use one of the sized types like int32_t. The sized types are in the C99 standard, and they'll probably make it into the C++ standard eventually too.

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: integer type, still machine dependent?

    The fact that the basic types (amongst other things) are machine dependent are one of the reasons people use C

  4. #4
    Join Date
    Nov 2009
    Posts
    2

    Re: integer type, still machine dependent?

    Hi,

    Thanks a lot for trying to help.

    I'm trying to write clean code, and I'm confused. So when should we use each of them?

    monarch_dodra, I don't get it, what's the advantage?

  5. #5
    Join Date
    Nov 2007
    Location
    Birmingham, England
    Posts
    157

    Re: integer type, still machine dependent?

    This discussion came up a few days ago. http://www.codeguru.com/forum/showthread.php?t=488068

    The general upshot is that all types are machine dependent in size. The order of sizes is strictly defined. If you have requirements for the size of your variables you're safest bet is to use sizeof() to test the sizes of each type (remember there is a "long long" type supported by many compilers, not all). Alternatively most compilers support __int8 etc to make it unambiguous.
    Signature
    Please use: [ code ][/ code ] tags and reasonably correct tabbing. They really help us read your code
    End Signature

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: integer type, still machine dependent?

    Quote Originally Posted by Ahmedg View Post
    Hello everyone,

    I hope my question is not so silly, but when I started to learn C-language 10 years ago, books was mentioning that the data type "int" is machine dependent,
    From what I remember, nothing stops a compiler from "faking out" any number of bits it wants to for an int. For example, I was using C and C++ compilers that had 32-bit ints when programming for 16-bit MSDOS. Special run-time library routines were responsible in getting the 32-bit ints to work in this mode.

    Of course, it is more natural for a compiler to support the actual bitness of the machine, but nothing really stops a compiler from implementing 64 bit ints, 128 bit ints, etc. regardless of the machine.

    Regards,

    Paul McKenzie

  7. #7
    Join Date
    Jan 2009
    Posts
    1,689

    Re: integer type, still machine dependent?

    I don't think anything is machine dependent, I think you mean compiler dependent.

    int in MinGW is always 32-bit, regardless of machine. Most C++ compilers have their ints 32-bit, it's just not assured. The best thing to do it do define your own int type in preprocessor

    Code:
    #define int_32 int
    
    ASSERT(sizeof(int_32) == 4, "Oops, add a #if for this platform")

  8. #8
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: integer type, still machine dependent?

    Good use for a boost::static_assert there.

  9. #9
    Join Date
    Jan 2009
    Posts
    1,689

    Re: integer type, still machine dependent?

    Not sure how many compilers support it, but GNU does:

    int32_t

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured