Reputation: 23
I'm new to C++. I was learning about types, their memory uses and the differences in their memory size based on architecture. Is there any downside to using fixed-width types such as int32_t?
Upvotes: 2
Views: 737
Reputation: 5944
E.g.
To save space, use int_least32_t
To save time, use int_fast32_t
But in actuality, I personally use long
(at least 32-bit) and int
(at least 16-bit) from time to time simply because they are easier to type.
(Besides, int32_t
is optional, not guaranteed to exist.)
Upvotes: 0
Reputation: 308138
The original intent of the int
type was for it to represent the natural size of the architecture you were running on; you could assume that any operations on it were the fastest possible for an integer type.
These days the picture is more complicated. Cache effects or vector instruction optimization might favor using an integer type that is smaller than the natural size.
Obviously if your algorithm requires an int
of at least a certain size, you're better off being explicit about it.
Upvotes: 2
Reputation: 224884
The only real downside might be if you want your code to be portable to a system that doesn't have a 32-bit integer type. In practice those are pretty rare, but they are out there.
C++ has access to the C99 (and newer) integer types via cstdint
, which will give you access to the int_leastN_t
and int_fastN_t
types which might be the most portable way to get specific bit-widths into your code, should you really happen to care about that.
Upvotes: 4