PSkocik PSkocik - 1 year ago 93
C Question

Standard and fasted integer equivalency

The standard gives the following minimum bit widths for standard unsigned types:

unsigned char >= 8
unsigned short >= 16
unsigned int >= 16
unsigned long >= 32
unsigned long long >= 64

(Implicitly, by specifying minimum maximal values).

Does that imply the following equivalencies?

unsigned char == uint_fast8_t
unsigned short == uint_fast16_t
unsigned int == uint_fast16_t
unsigned long == uint_fast32_t
unsigned long long == uint_fast64_t

Answer Source

No, because the size of the default "primitive data types" are picked by the compiler to be something convenient for the given system. Convenient meaning easy to work with for various reasons: integer range, the size of the other integer types, backwards compatibility etc. It is not necessarily picked to be the fastest possible.

For example, in practice unsigned int has the size 32 on a 32 bit system, but also size 32 on a 64 bit system. But on the 64 bit system, uint_fast32_t could be 64 bits.

Just look at the most commonly used sizes in practice for unsigned char versus uint_fast8_t:

Data bus   unsigned char   uint_fast8_t
8 bit      8 bit           8 bit
16 bit     8 bit           16 bit
32 bit     8 bit           32 bit
64 bit     8 bit           32/64 bit

This is so, because for convenience we need a byte type to work with. Yet the optimizing compiler may very well place this unsigned char byte at an aligned address and read it as a 32 bit access, so it might perform optimizations despite the actual size of the data type.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download