Portable Datatypes (C++)

Started by
17 comments, last by JasonBlochowiak 17 years, 8 months ago
I remember seeing datatypes such as int16, uint16, int32, uint32, ect ect in some libs but I cant remember them and I was wondering if anyone has any info on this. Is it just a mere typedef, surely not? Im guessing you manually allocate the size, does anyone have a already built lib for this? Also, im looking for an arbituary number lib (big nums), any suggestions on which I should use? Thanks
Advertisement
In linux (gcc) there is usually a stdint.h with typedefs for those, and there may be something on windows.

I usually just make my own...

typedef unsigned char      uint8_ttypedef char               int8_t;typedef unsigned short     uint16_t;typedef signed short       int16_t;typedef unsigned int       uint32_t;typedef signed int         int32_t;// etc


Just have that header on any platform you compile for.

EDIT: Here's some documentation about stdint.h:
but those arnt guarenteed to be the size you say they are...int isnt always 32! Im looking for something that guarenteed.
That's why I said you have to provide those 8 lines or whatever on any platform you build on. THat way, the other many millions of lines of code can use uint32_t, etc, and you don't have to recode everything.

Trust me, this is how it works. Read the link I posted on the first post in the edit.

EDIT: I worded it awkwardly... every platform must have a platform specific version of those typedefs.
Alright, thanks a lot.
I know that in VC++ 2003 and higher, there are size specific types

__int8, __int16, __int32, __int64

not sure if they are standardized across different compilers though (probably arent)
Boost has <boost/cstdint.hpp>, which is perhaps your best choice so far. C99 has <stdint.h>, which will become <cstdint> in C++0x, but that's a few years off.

Of course, you could always just use a good serialisation method that doesn't rely on specific type sizes.
Doing your own typedef's is the way to go. That way if you decide to target another platform all you have to do is change the typedef's around to suit the new platform.
-----------------------------------Indium Studios, Inc.
I tried using fixed size int8,int16,etc religiously when I was a junior programmer. In the end it did not work out well. Sooner or later you will have to use API's and then you can set yourself up for all sorts of type-casting headaches. I wound up just picking the dominant API and stuck to its type convention. If you really need to get byte sizes right for file formats or protocols, WORD is pretty much guaranteed to be 16 bits.
Quote:Original post by SoftwareGuy256
[...]WORD is pretty much guaranteed to be 16 bits.
Only if you define the type to be 16 bits just like smitty1276 said. Personally, it really does seem better to use a pre-made system like boost::integer since it's already been extensively tested on most platforms you're likely to ever deal with (excepting those yet to be released - you have to wait a short while after release to be sure those are well-tested).
"Walk not the trodden path, for it has borne it's burden." -John, Flying Monk

This topic is closed to new replies.

Advertisement