Cross-platform primitive data types in C++

前提是你 提交于 2020-01-01 04:33:05

问题


Unlike Java or C#, primitive data types in C++ can vary in size depending on the platform. For example, int is not guaranteed to be a 32-bit integer. Various compiler environments define data types such as uint32 or dword for this purpose, but there seems to be no standard include file for fixed-size data types.

What is the recommended method to achieve maximum portability?


回答1:


I found this header particularly useful: BOOST cstdint

Usually better than inventing own wheel (which incurs the maintenance and testing).




回答2:


Create a header file called types.h, and define all the fixed-size primitive types you need (int32, uint32, uint8, etc.). To support multiple platforms, you can either use #ifdef's or have a separate include directory for each platform (include_x86, include_x86_64, include_sparc). In the latter case you would have separate build configurations for each platform, which would have the right include directory in their include path. The second method is preferable, according to the "The C++ Gotchas" by Stephen Dewhurst.

Just an aside, if you are planning to pass binary data between different platforms, you also have to worry about byte order.




回答3:


Part of the C99 standard was a stdint.h header file to provide this kind of information. For instance, it defines a type called uint32_t. Unfortunately, a lot of compilers don't support stdint.h. The best cross-platform implementation I've seen of stdint.h is here: http://www.azillionmonkeys.com/qed/pstdint.h. You can just include that in your project.

If you're using boost, I believe it also provides something equivalent to the stdint header.




回答4:


Define a type (e.g. int32) in a header file. For each platform use another #ifdef and make sure that in32 is a 32 bit integer. Everywhere in your code use int32 and make sure that when you compile on different platforms you use the right define




回答5:


Two things:

First, there is a header file called limits.h that gives lots of useful platform specific information. It will give max and min values for the int type for example. From that, you can deduce how big the int type is.

You can also use the sizeof operator at runtime for these purposes too.

I hope this helps . . .

K




回答6:


If its name begins with two underscores (__), a data type is non-standard.

__int8 (unsigned __int8)

__int16 (unsigned __int16)

__int32 (unsigned __int32)

__int64 (unsigned __int64)

Try to use boost/cstdint.hpp




回答7:


There is a stdint.h header defined by the C99 standard and (I think) some variant or another of ISO C++. This defines nice types like int16_t, uint64_t, etc... which are guaranteed to have a specific size and representation. Unfortunately, it's availability isn't exactly standard (Microsoft in particular was a foot dragger here).

The simple answer is this, which works on every 32 or 64 bit byte-addressable architecture I am aware of:

  • All char variables are 1 byte
  • All short variables are 2 bytes
  • All int variables are 4 byte
  • DO NOT use a "long", which is of indeterminate size.
  • All known compilers with support for 64 bit math allow "long long" as a native 64 bit type.

Be aware that some 32 bit compilers don't have a 64 bit type at all, so using long long will limit you to 64 bit systems and a smaller set of compilers (which includes gcc and MSVC, so most people won't care about this problem).



来源:https://stackoverflow.com/questions/1385709/cross-platform-primitive-data-types-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!