Understanding fixed width integer types

940 Views Asked by At

I understand the idea of fixed width types, but I am little confused by the explanation provided by the reference:

signed integer type with width of exactly 8, 16, 32 and 64 bits respectively with no padding bits and using 2's complement for negative values (provided only if the implementation directly supports the type)

So as far as I understand, if I was able to compile an application, everything should work on platforms which are able to run it. There are my questions:

  1. What if some platform does not provide support for those types? Is some kind of alignment used or can the application not at all?
  2. If we have a guarantee that sizeof(char) is exactly one byte on every platform, regardless of byte size which can be different among platforms, does it mean that int8_t and uint8_t are guaranteed to be available everywhere?
1

There are 1 best solutions below

4
On

If the implementation does not provide the type you used, it will not exist and your code will not compile. Manual porting will be needed in this case.

Regarding your second question: while we know that sizeof(char) == 1, it is not guaranteed that char has exactly eight bits; it can have more than that. If that is the case int8_t and friends will not exist.

Note that there are other types that might provide sufficient guarantees for your use case if you don't need to know the exact width, such as int_least8_t or int_fast8_t. Those leave the implementation some more freedom, making them more portable.

However, if you are targeting a platform on which common integer types do not exist, you should know that in advance anyway; so it is not worth spending too much time working around those most likely irrelevant issues. Those platforms are relatively exotic.