Are sized int's (like "int[size]_t") generally more efficient than a standard int, assuming the right size is being chosen?
Depends on what you mean with efficiency. In terms of memory, you know how much space they take up, so they are generally more memory-efficient. There is also the uint_leastn_t
types that can supposedly be used for memory optimizations (pick the smallest available type that can store at least n bits).
In terms of speed, aligned access might be a concern. The int
type usually corresponds to the alignment of the CPU, so it might be faster than lets say uint16_t
, but this is of course highly system-specific.
The C standard is fully aware of this issue though, so if speed is a concern you should use the uint_fastn_t
types of cstdint.h, for example uint_fast32_t
. Then you will get the fastest possible type with at least n bits.
which data type out of "int[size]_t", "__int[size]" and "int[size]" should I use, with efficiency and compatibility between different computer systems in mind?
- For maximum portability you should use a fixed size type such as
uint32_t
.
- For maximum speed efficiency, you should use the "fast" types such as
uint_fast32_t
.
- For rare cases of memory efficiency optimizations, you can use the "least" types such as
uint_least32_t
.
- There is rarely ever any reason to use
int
. Mostly this is for backwards-compatibility with old code.
- There is no reason whatsoever to use non-standard
__int32
.
Language lawyers will post comments saying that stdint.h is no longer mandatory, but no sane person will use a new compiler that doesn't support it.