我正在寻找关于基本c++类型大小的详细信息。
我知道这取决于架构(16位、32位、64位)和编译器。
但是c++有标准吗?
我在32位架构上使用Visual Studio 2008。以下是我得到的答案:
char : 1 byte
short : 2 bytes
int : 4 bytes
long : 4 bytes
float : 4 bytes
double: 8 bytes
我试图在不同的架构和编译器下找到char、short、int、long、double、float(以及其他我没有想到的类型)的大小的可靠信息,但没有多大成功。
实际上没有这样的事情。通常,std::size_t表示当前体系结构上的无符号本机整数大小。即16位、32位或64位,但并不总是如此,就像这个答案的评论中指出的那样。
至于所有其他内置类型,它实际上取决于编译器。以下是摘自最新c++标准的当前工作草案的两段摘录:
There are five standard signed integer types : signed char, short int, int, long int, and long long int. In this list, each type provides at least as much storage as those preceding it in the list.
For each of the standard signed integer types, there exists a corresponding (but different) standard unsigned integer type: unsigned char, unsigned short int, unsigned int, unsigned long int, and unsigned long long int, each of which occupies the same amount of storage and has the same alignment requirements.
如果您愿意,您可以静态(编译时)断言这些基本类型的sizeof。如果假设的大小发生变化,它会提醒人们考虑移植您的代码。
正如其他人回答的那样,“标准”都将大部分细节保留为“实现定义的”,只声明类型“char”的宽度至少为“char_bis”,并且“char <= short <= int <= long <= long long”(浮点数和双精度浮点数与IEEE浮点标准基本一致,长双精度浮点数通常与双精度浮点数相同——但在更当前的实现中可能更大)。
Part of the reasons for not having very specific and exact values is because languages like C/C++ were designed to be portable to a large number of hardware platforms--Including computer systems in which the "char" word-size may be 4-bits or 7-bits, or even some value other than the "8-/16-/32-/64-bit" computers the average home computer user is exposed to. (Word-size here meaning how many bits wide the system normally operates on--Again, it's not always 8-bits as home computer users may expect.)
If you really need a object (in the sense of a series of bits representing an integral value) of a specific number of bits, most compilers have some method of specifying that; But it's generally not portable, even between compilers made by the ame company but for different platforms. Some standards and practices (especially limits.h and the like) are common enough that most compilers will have support for determining at the best-fit type for a specific range of values, but not the number of bits used. (That is, if you know you need to hold values between 0 and 127, you can determine that your compiler supports an "int8" type of 8-bits which will be large enought to hold the full range desired, but not something like an "int7" type which would be an exact match for 7-bits.)
注意:使用了许多Un*x源包”。/configure”脚本,它将探测编译器/系统的功能,并输出一个合适的Makefile和config.h。您可以检查其中一些脚本,看看它们是如何工作的,以及它们如何探测编译器/系统功能,并遵循它们的指导。
我注意到这里所有的其他答案几乎都集中在整型上,而提问者也问了浮点数。
我不认为c++标准需要它,但是现在大多数常见平台的编译器通常都遵循IEEE754标准的浮点数。该标准指定了四种类型的二进制浮点数(以及一些BCD格式,我从未见过c++编译器支持这些格式):
半精度(binary16) - 11位有效值,指数范围-14到15
单精度(binary32) - 24位有效值,指数范围-126至127
双精度(binary64) - 53位有效值,指数范围-1022到1023
四倍精度(binary128) - 113位有效值,指数范围-16382到16383
那么,这是如何映射到c++类型的呢?一般浮子采用单精度;因此,sizeof(float) = 4。然后double使用双精度(我相信这是double名称的来源),长double可能是双精度或四倍精度(在我的系统上是四倍精度,但在32位系统上可能是双精度)。我不知道有哪个编译器能提供半精度浮点数。
总结一下,通常是这样的:
Sizeof (float) = 4
Sizeof (double) = 8
Sizeof (long double) = 8或16
c++标准没有以字节为单位指定整型的大小,但它指定了它们必须能够容纳的最小范围。您可以从所需的范围推断出最小大小(以位为单位)。您可以从该值和CHAR_BIT宏的值推断出最小的字节大小,CHAR_BIT宏定义了字节中的位数(除了最晦涩的平台之外,在所有平台中它都是8,而且不能小于8)。
char的另一个限制是它的大小总是1字节,或CHAR_BIT位(因此得名)。
标准(第22页)要求的最小范围是:
MSDN上的数据类型范围:
signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement platforms)
unsigned char: 0 to 255
"plain" char: -127 to 127 or 0 to 255 (depends on default char signedness)
signed short: -32767 to 32767
unsigned short: 0 to 65535
signed int: -32767 to 32767
unsigned int: 0 to 65535
signed long: -2147483647 to 2147483647
unsigned long: 0 to 4294967295
signed long long: -9223372036854775807 to 9223372036854775807
unsigned long long: 0 to 18446744073709551615
A C++ (or C) implementation can define the size of a type in bytes sizeof(type) to any value, as long as
表达式sizeof(type) * CHAR_BIT计算为足够包含所需范围的比特数,并且
类型的顺序仍然有效(例如sizeof(int) <= sizeof(long))。
实际的特定于实现的范围可以在C或c++的header中找到(或者更好的是,在header中找到模板化的std::numeric_limits)。
例如,这是你如何找到int的最大范围:
C:
#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;
C++:
#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();
这是正确的,但是,你说的也对:
Char: 1字节
短:2字节
Int: 4字节
Long: 4字节
浮点数:4字节
Double: 8字节
因为32位体系结构仍然是默认的,也是最常用的,并且自从前32位时代内存可用性较低以来,他们就一直保持这些标准大小,为了向后兼容和标准化,它保持不变。即使是64位系统也倾向于使用这些并进行扩展/修改。
更多信息请参考:
http://en.cppreference.com/w/cpp/language/types