Fixed sized datatypes.

Discussion in 'C++' started by Justin Barrett, Dec 28, 2004.

  1. Hi I'm currently doing a project in my free time. One problem I ran into
    is that things like short, int, and long all have different meanings as to
    size on different platforms.

    In this program it's pretty important that some values only be 16-bits in
    length. Is there a way to do this and make it portable?

    Thanks in advance,
    JB
     
    Justin Barrett, Dec 28, 2004
    #1
    1. Advertisements

  2. 'unsigned short' is guaranteed to have _at_least_ 16 bits. If you need
    _exactly_ 16 bits, you are out of luck. There are platforms out there
    that simply don't have any types smaller than 32 bits. Your code could
    never be ported there. If you don't care about that, you can settle on
    making it "portable" to only those platforms that do have _a_ type which
    is exactly 16 bits. Once you decide that that's what you want, you simply
    create a header with conditionally compiled sections each of which defines
    its own "justin_16bit_signed" type:

    #ifdef WIN32
    typedef short justin_16bit_signed;
    typedef unsigned shot justin_16bit_unsigned;
    #else
    ....

    Victor
     
    Victor Bazarov, Dec 28, 2004
    #2
    1. Advertisements

  3. Justin Barrett

    David White Guest

    You can create a platform-specific header file containing typedefs for
    fixed-sized types, e.g.,
    typedef short Int16;
    typedef unsigned short Uint16;

    Use only the Int16 or Uint16 types in your code where you need 16-bit types.
    When you move to a different platform, alter the header file as necessary
    and re-compile.

    DW
     
    David White, Dec 28, 2004
    #3
  4. Justin Barrett

    Pete Becker Guest

    This should be conditionalized on the compiler, not the OS.

    #ifdef _MSC_VER
    ....
     
    Pete Becker, Dec 28, 2004
    #4
  5. This is very *bad* advice.
    Use the [C99] standard header file

    #include <stdint.h>

    instead.
    Then your code will port to every platform
    that supports a C99 compiler
    and integers of the sizes that you need.
     
    E. Robert Tisdale, Dec 28, 2004
    #5
  6. Thanks for all of your advance. It really helped me out.

    Justin
     
    Justin Barrett, Dec 29, 2004
    #6
  7. I do not agree, but I am not going to argue. It's up to the
    programmer what to conditionalize on. For all we know it could
    be conditionalized on some custom macro defined by the programmer
    in the project settings.

    V
     
    Victor Bazarov, Dec 29, 2004
    #7
  8. Justin Barrett

    David White Guest

    Why is it "*bad*" advice? It would work perfectly well. I agree that if
    there's support for fixed-sized types provided by the language standard,
    then that would be preferable, but your program isn't going to suddenly
    crash or disappear in a puff of smoke if you follow my advice.
    Is this a C or C++ newsgroup? I can find no mention of stdint.h in the ISO
    14882 C++ standard.

    DW
     
    David White, Dec 29, 2004
    #8
  9. Justin Barrett

    Jack Klein Guest

    As at least one other poster suggested, do a Google search for the C
    standard <stdint.h> header. Now others have pointed out that this was
    added in the 1999 update to the C language standard, and is not
    actually part of the C++ standard at all, and they are correct but
    somewhat short-sighted.

    But without a doubt it will be added to the next major update to the
    C++ standard, probably as both <stdint.h> and <cstdint.h>. And right
    now, quite a few compilers supply this header already, and it works
    when they compile C++ programs as well as when compiling C programs.

    Even if you can't find one already put together for your
    implementation, you should either be able to write one from scratch or
    find one similar enough that you can modify it.

    Assuming that your C++ implementation provides 2's complement integer
    types with widths of exactly 8, 16, and 32 bits, and I'll bet it does,
    you can write your own <stdint.h> header that will be 100% C++
    standard conforming except for the 64 bit integer types.

    It's not that hard at all. I've done it for several C and C++
    compilers.
     
    Jack Klein, Dec 29, 2004
    #9
  10. Probably as both <stdint.h> and <cstdint> (no .h)

    V
     
    Victor Bazarov, Dec 29, 2004
    #10
  11. Justin Barrett

    Pete Becker Guest

    The point still remains, however: the size of an integral type is
    determined by the compiler, so whatever name is used for a magic macro,
    ultimately the choice has to be based on the compiler that's being used.
     
    Pete Becker, Dec 29, 2004
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.