Appropriate selection of integer types, cross-platform

Started by
8 comments, last by roos 18 years, 8 months ago
I'm wondering what integer types people typically use here rather than just the standard uint and int. The size of the integer (4 bytes, 8 bytes, etc.) can change from system to system and make it easy for your game to work on one system but totally flop on another. What one needs is a set of integer types that are guaranteed to be the same size across all systems. So basically what I'm looking for is a set of integer types that: - have a static size - are available on multiple architectures and operating systems (my game is cross-platform) - (preferrably) are supported natively on major compilers At work I was introduced to using the types defined in inttypes.h. I was able to compile my source using those types on both x86 and PowerPC with GCC, but I don't know if that header is something specific to the GNU C libraries or not. Basically to use it you would do this (C++):

#include <inttypes.h>

int32_t var1;  // A signed 32-bit integer
uint32_t var2; // An unsigned 32-bit integer
And there are other sizes as well of course. Anyone have experience to share with variable type selection? EDIT: By the way my game uses SDL, which I think uses types like Uint32...? [Edited by - Roots on August 17, 2005 9:26:34 PM]

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

Advertisement
If your compiler supports C99 you can probably just use stdint.h, if not that you can try out boost/cstdint.hpp.
The C99 standard provides fixed sized integral types. As of this moment standard C++ does not incorporate them (its being considered & reviewed as we speak) but some compilers provide them as an extension or provide some form of fixed sized integral types so if you can get your hands on the boost library they have a a cross platform implementation of C99 fixed sized integral types for C++ check here
Quote:Original post by snk_kid
The C99 standard provides fixed sized integral types. As of this moment standard C++ does not incorporate them (its being considered & reviewed as we speak) but some compilers provide them as an extension or provide some form of fixed sized integral types so if you can get your hands on the boost library they have a a cross platform implementation of C99 fixed sized integral types for C++ check here


Hmm, well if possible I would like a solution that didn't require introducing a huge code library as a dependance (especially if we're only using it for one or two small things). Thanks for the info though.


On a side note, I noticed you have a quote from School Rumble in your sig. I translated almost all of the School Rumble manga for <a href="http://www.yanime.com/>Yanime by the way. [grin]

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

I've found that as long as I can refer to the bounds properly [ala numeric_limits<foo>] I often don't care about the size of the type.
Quote:Original post by Telastyn
I've found that as long as I can refer to the bounds properly [ala numeric_limits<foo>] I often don't care about the size of the type.


When using/computing values this is often true. However, when you want to serialize data in a binary form having the sizes be consistent across platforms comes in mighty handy.

-John
- John
You can probably just gang the cstdint.hpp file [and possibly a handful of support files] without actually including all of boost in your project.

Failing that, it is easily enough mimicked on your own via typedefs:
#ifdef INTTYPES_H_IS_AVAILABLE   #include <inttypes.h>#else#ifdef _MSC_VER=6 //I'm making this up, I don't recall the proper definition   typedef int32 int32_t;#endif

Throw that into a custom types file, and extend as needed.

It is much too late, so I'm drawing a complete blank on the proper preprocessor syntax, so apologies if that's wrong.

CM
If you're adverse to the idea of using boost, then something like this will suffice:

#if MSC_VER //constant defined by VC++    typedef signed char Int8;    ...    typedef __int64 Int64;#else if SOME_OTHER_COMPILER_IDICATING_CONSTANT    ...    typedef whatever Int64;#else    #error#endif


but I strongly suggest you use boost
Boost isn't a library, it's a collection of libraries. Using some library which is part of the boost collection doesn't mean your project depends on "one big monolithic library" that is called boost.


// ville
Why not just use SDL's types?

This topic is closed to new replies.

Advertisement