我正在寻找关于基本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(以及其他我没有想到的类型)的大小的可靠信息,但没有多大成功。
我们可以为类型定义同义词,这样我们就可以创建自己的“标准”。
在sizeof(int) == 4的机器上,我们可以定义:
typedef int int32;
int32 i;
int32 j;
...
所以当我们把代码转移到另一台机器上,当long int的大小是4时,我们可以重新定义int的一次出现。
typedef long int int32;
int32 i;
int32 j;
...
我注意到这里所有的其他答案几乎都集中在整型上,而提问者也问了浮点数。
我不认为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
正如其他人回答的那样,“标准”都将大部分细节保留为“实现定义的”,只声明类型“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。您可以检查其中一些脚本,看看它们是如何工作的,以及它们如何探测编译器/系统功能,并遵循它们的指导。
有标准。
C90标准要求
sizeof(short) <= sizeof(int) <= sizeof(long)
C99标准要求
sizeof(short) <= sizeof(int) <= sizeof(long) <= sizeof(long long)
这是C99的规格。第22页详细介绍了不同整型的大小。
下面是Windows平台的int类型大小(位):
Type C99 Minimum Windows 32bit
char 8 8
short 16 16
int 16 32
long 32 32
long long 64 64
如果你关心可移植性,或者你想要类型的名称反映大小,你可以查看头文件<inttypes.h>,其中有以下宏:
int8_t
int16_t
int32_t
int64_t
Int8_t保证为8位,int16_t保证为16位,以此类推。
当涉及到不同架构和不同编译器的内置类型时,只需在你的架构上用编译器运行以下代码,看看它输出了什么。下面是我的Ubuntu 13.04 (Raring Ringtail) 64位g++4.7.3输出。还请注意下面的回答,这就是为什么输出是这样排序的:
有五种标准的有符号整型:有符号char、short int、int、long int和long long int。在此列表中,每种类型提供的存储空间至少与列表中前面的类型相同。”
#include <iostream>
int main ( int argc, char * argv[] )
{
std::cout<< "size of char: " << sizeof (char) << std::endl;
std::cout<< "size of short: " << sizeof (short) << std::endl;
std::cout<< "size of int: " << sizeof (int) << std::endl;
std::cout<< "size of long: " << sizeof (long) << std::endl;
std::cout<< "size of long long: " << sizeof (long long) << std::endl;
std::cout<< "size of float: " << sizeof (float) << std::endl;
std::cout<< "size of double: " << sizeof (double) << std::endl;
std::cout<< "size of pointer: " << sizeof (int *) << std::endl;
}
size of char: 1
size of short: 2
size of int: 4
size of long: 8
size of long long: 8
size of float: 4
size of double: 8
size of pointer: 8
有一个标准,它是在各种标准文档(ISO, ANSI等)中指定的。
维基百科有一个很好的页面解释了各种类型和它们可以存储的最大值:
计算机科学中的整数。
然而,即使使用标准的c++编译器,您也可以使用以下代码片段相对容易地找到:
#include <iostream>
#include <limits>
int main() {
// Change the template parameter to the various different types.
std::cout << std::numeric_limits<int>::max() << std::endl;
}
std::numeric_limits的文档可以在Roguewave上找到。它包含大量其他命令,您可以调用这些命令来查找各种限制。这可以用于任何传递大小的任意类型,例如std::streamsize。
约翰的回答包含了最好的描述,因为那些肯定是成立的。不管你在什么平台上,有另一个很好的页面详细介绍了每种类型必须包含多少位:int类型,这是在标准中定义的。
我希望这能有所帮助!