我正在寻找关于基本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。您可以检查其中一些脚本,看看它们是如何工作的,以及它们如何探测编译器/系统功能,并遵循它们的指导。

有一个标准,它是在各种标准文档(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类型,这是在标准中定义的。

我希望这能有所帮助!

不,字体大小没有标准。标准只要求:

sizeof(short int) <= sizeof(int) <= sizeof(long int)

如果你想要一个固定大小的变量,你能做的最好的事情就是像这样使用宏:

#ifdef SYSTEM_X
  #define WORD int
#else
  #define WORD long int
#endif

然后可以使用WORD定义变量。不是说我喜欢这个,而是说这是最方便的方式。

如果你需要固定大小的类型,使用类型像uint32_t(32位无符号整数)定义在stdint.h。它们在C99中指定。

对于32位系统,“事实上”的标准是ILP32——也就是说,int、long和pointer都是32位的量。

对于64位系统,主要的Unix“事实上”标准是LP64 -长和指针是64位(但int是32位)。Windows 64位标准是LLP64 - long long和pointer是64位(但long和int都是32位)。

曾经,一些Unix系统使用ILP64组织。

这些事实上的标准没有一个是由C标准(ISO/IEC 9899:1999)立法的,但都是被C标准所允许的。

并且,根据定义,sizeof(char)是1,尽管在Perl配置脚本中进行了测试。

注意,有些机器(Crays) CHAR_BIT远大于8。这意味着,IIRC sizeof(int)也是1,因为char和int都是32位的。