1

I'm developing a multi platform app (iOS, Android, etc), using C++.

Are there base types in the C++ standard which are guaranteed to be a fixed width, and portable across multiple platforms?

I'm looking for fixed-width types such as Int32, UInt32, Int16, UInt16, Float32, etc.

BTownTKD
  • 7,911
  • 2
  • 31
  • 47
Rahul Iyer
  • 19,924
  • 21
  • 96
  • 190
  • 4
    C++ has neither of those. – chris Jun 25 '14 at 14:16
  • 1
    There *is* an `int32_t`, though. For `float`, see [this](http://stackoverflow.com/questions/2524737/fixed-size-floating-point-types) – dlf Jun 25 '14 at 14:18
  • 1
    @dlf There _may_ be an `int32_t`. It's not present on all platforms. (But the real question is: why does he want fixed width types? There's almost no reason to use them.) – James Kanze Jun 25 '14 at 14:39
  • I was reading that an upcoming version of android is going to be 64bit. The new iOS devices are also 64bit (even though only a few data types have different sizes - CGFloat, pointer sizes, long). I'm writing my first major C++ project which is multi platform and I don't have much C++ experience, so my questions may not make sense. XP I was just trying to plan ahead in case there is a major change after I release my app. – Rahul Iyer Jun 25 '14 at 15:03
  • @JamesKanze: Embedded systems, such as mobile phones, may need bit width specific data types. For example, I don't believe that Android OS provides a 32-bit API to the ADC (microphone). I haven't see any ADCs that are more than 16-bits. – Thomas Matthews Jun 25 '14 at 16:23
  • @ThomasMatthews Embedded systems are sometimes a bit special. Although even then, you'd only really need the exact sized types when addressing memory mapped IO, and even then... Since you're not portable, if `short` is 16 bits... – James Kanze Jun 25 '14 at 16:47

4 Answers4

3

int32 is a custom typedef, only int exists by default. If you need a specified width take a look at stdint.h

#include <cstdint>

int32_t integer32bits;

I don't think any floating point counterpart exists in the standard, correct me if I'm wrong.

Jack
  • 131,802
  • 30
  • 241
  • 343
  • Don't forget `int32_t` is optional, so to answer the question, probably not the same on all platforms. Same size for all that include it maybe. – chris Jun 25 '14 at 14:19
  • 1
    [This question](http://stackoverflow.com/questions/2524737/fixed-size-floating-point-types) addresses the issue for floats. – dlf Jun 25 '14 at 14:20
  • 1
    @chris Yeah, though a compiler error is far better than a different size, so "not the same on all platforms" is misleading. –  Jun 25 '14 at 14:21
  • Seems to only exist in C++11; if OP needs to support specific embedded platforms, those typedefs may not exist yet. – BTownTKD Jun 25 '14 at 14:21
  • 1
    @BTownTKD It's not describes in C++98 AFAIK but it has existed for a while in C and many major compilers support it as an extension with semantics identical to the C++11 version. But yes, some embedded toolchains don't support it. Nothing new though, it's always been (and will remain for a long time) optional so even a full C++11 toolchain might omit it. –  Jun 25 '14 at 15:07
1

Floats are almost always 32 bit except on some obscure platforms that do not comply with IEEE 754. You don't need to bother with those, in all likelihood. Integer types may vary, but if your target platform has a C++11-compliant compiler, then you can use the cstdint header to access types of a specific size in a standard way. If you can't use C++11, then you will need separate code for each platform, most likely.

More Axes
  • 249
  • 3
  • 12
1

The definitions in <stdint.h>, or <cstdint> can be used for portability:

  • int32_t is guaranteed to be a typedef for a signed 32 bit type, or not exist at all. Since this is C++, you can use enable_if to decide on a course of action.
  • int_least32_t is a typedef for the smallest type that has at least 32 bits
  • int_fast32_t is a typedef for a type that has at least 32 bit and can be operated on efficiently (e.g. if the memory bus is 64 bit wide and allows no partial stores, it is faster to use a 64 bit type and waste memory rather than perform read-modify-write accesses)

See also The difference of int8_t, int_least8_t and int_fast8_t.

Note that different systems can also have different endianness, so it is never safe to transmit these over the network.

Community
  • 1
  • 1
Simon Richter
  • 28,572
  • 1
  • 42
  • 64
  • 1
    Or even save them to disk. I've seen the byte order of `long` change when I upgraded a compiler. And of course, disks are often mounted remotely, and some of the other machines on the network will invariably have a different representation of `int`. – James Kanze Jun 25 '14 at 14:41
1

C++ has no such type int32, so I will assume you are referring to int32_t. And to clear up some terminology, width usually refers to the number of bits wide a type is, and size refers to the number of bytes or chars a type is wide.

Yes, an int32_t is required to be the same width, i.e., number of bits wide, on all platform that supports it, by definition. Exactly 32 bits wide. Do keep in mind that the type is optional and might not be defined at all, if and only if, the system does not have such a type, for example, if 32 is not a multiple of CHAR_BIT.

But their size in bytes (as returned by the sizeof() operator) depends on the value of CHAR_BIT. That is, the number of bits in a byte. On most systems, a byte is 8 bits, so sizeof(int32_t) will yield 4. But on an exotic system with 16-bit bytes, sizeof(int32_t) will be only 2.

user16217248
  • 3,119
  • 19
  • 19
  • 37