Jump to content
  • Advertisement
Sign in to follow this  
Azh321

Portable Datatypes (C++)

This topic is 4351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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_t
typedef 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:

Share this post


Link to post
Share on other sites
but those arnt guarenteed to be the size you say they are...int isnt always 32! Im looking for something that guarenteed.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!