Fixed width integer types (since C++11)
type and range
越界问题非常频繁地困扰着开发人员
CPP常用的类型定义不能体现数据的bit位数,让开发人员非常抓狂,相信很多人都遇到过,当判定一个数值是否越界时候,一定是先去网上查表,
但是数据类型对应的范围,依赖具体的compiler的编译器实现的, 嵌入式系统往往范围比较小, 服务器系统编译范围一般按照如下标准的来。
https://www.geeksforgeeks.org/c-data-types/
Data Type Size (in bytes) Range short int 2 -32,768 to 32,767 unsigned short int 2 0 to 65,535 unsigned int 4 0 to 4,294,967,295 int 4 -2,147,483,648 to 2,147,483,647 long int 4 -2,147,483,648 to 2,147,483,647 unsigned long int 4 0 to 4,294,967,295 long long int 8 -(2^63) to (2^63)-1 unsigned long long int 8 0 to 18,446,744,073,709,551,615 signed char 1 -128 to 127 unsigned char 1 0 to 255 float 4 double 8 long double 12 wchar_t 2 or 4 1 wide character
Note : Above values may vary from compiler to compiler. In the above example, we have considered GCC 32 bit.
与系统架构也产生迷惑
https://stackoverflow.com/questions/17489857/why-is-int-typically-32-bit-on-64-bit-compilers
Why is
int
typically 32 bit on 64 bit compilers? When I was starting programming, I've been taught int is typically the same width as the underlying architecture. And I agree that this also makes sense, I find it logical for a unspecified width integer to be as wide as the underlying platform (unless we are talking 8 or 16 bit machines, where such a small range forint
will be barely applicable).
Later on I learned
int
is typically 32 bit on most 64 bit platforms. So I wonder what is the reason for this. For storing data I would prefer an explicitly specified width of the data type, so this leaves generic usage forint
, which doesn't offer any performance advantages, at least on my system I have the same performance for 32 and 64 bit integers. So that leaves the binary memory footprint, which would be slightly reduced, although not by a lot...
这种设计跟与历史折中的相关。
The history, trade-offs and decisions are explained by The Open Group at http://www.unix.org/whitepapers/64bit.html. It covers the various data models, their strengths and weaknesses and the changes made to the Unix specifications to accommodate 64-bit computing.
Fixed width integer types
C11标准提出解决办法。
引入新的数据类型, 这种数据类型中添加bit位。
例如 int64_t
这种设计符合设计模式,见名知意。
https://en.cppreference.com/w/cpp/types/integer
Types
Defined in header<cstdint>
int8_tint16_tint32_tint64_t(optional)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 if and only if the implementation directly supports the type)
(typedef) int_fast8_tint_fast16_tint_fast32_tint_fast64_tfastest signed integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) int_least8_tint_least16_tint_least32_tint_least64_tsmallest signed integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) intmax_tmaximum-width signed integer type
(typedef) intptr_t(optional)signed integer type capable of holding a pointer to void
(typedef) uint8_tuint16_tuint32_tuint64_t(optional)unsigned integer type with width of exactly 8, 16, 32 and 64 bits respectively
(provided if and only if the implementation directly supports the type)
(typedef) uint_fast8_tuint_fast16_tuint_fast32_tuint_fast64_tfastest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) uint_least8_tuint_least16_tuint_least32_tuint_least64_tsmallest unsigned integer type with width of at least 8, 16, 32 and 64 bits respectively
(typedef) uintmax_tmaximum-width unsigned integer type
(typedef) uintptr_t(optional)unsigned integer type capable of holding a pointer to void