我正在寻找关于基本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(以及其他我没有想到的类型)的大小的可靠信息,但没有多大成功。


当前回答

c++标准没有以字节为单位指定整型的大小,但它指定了它们必须能够容纳的最小范围。您可以从所需的范围推断出最小大小(以位为单位)。您可以从该值和CHAR_BIT宏的值推断出以字节为单位的最小大小,CHAR_BIT宏定义了字节中的比特数。除了最不知名的平台,其他平台都是8,而且不能小于8。

char的另一个限制是它的大小总是1字节,或CHAR_BIT位(因此得名)。这在标准中有明确的说明。

C标准是c++标准的规范参考,所以即使它没有明确地说明这些要求,c++也要求C标准所要求的最小范围(第22页),这与MSDN上的数据类型范围相同:

signed char: -127 to 127 (note, not -128 to 127; this accommodates 1's-complement and sign-and-magnitude platforms) unsigned char: 0 to 255 "plain" char: same range as signed char or unsigned char, implementation-defined 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

c++(或C)实现可以用bytes sizeof(type)定义类型的大小为任何值,只要

表达式sizeof(type) * CHAR_BIT计算为足够高的位数,以包含所需的范围,并且 类型的顺序仍然有效(例如sizeof(int) <= sizeof(long))。

综上所述,我们可以保证:

Char,有符号Char和无符号Char至少是8位 有符号short、无符号short、有符号int和无符号int至少是16位 有符号长和无符号长至少是32位 有符号long long和无符号long long至少是64位

对于float或double类型的大小没有任何保证,除非double类型提供的精度至少与float类型相同。

实际的特定于实现的范围可以在C中的<limits.h>头文件中找到,或者在c++中的<climits>头文件中找到(或者更好的是,在<limits>头文件中找到模板化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();

其他回答

你可以使用OpenGL、Qt等库提供的变量。

例如,Qt提供了qint8(保证在Qt支持的所有平台上都是8位的)、qint16、qint32、qint64、quint8、quint16、quint32、quint64等等。

在64位机器上:

int: 4
long: 8
long long: 8
void*: 8
size_t: 8

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

正如其他人回答的那样,“标准”都将大部分细节保留为“实现定义的”,只声明类型“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位,以此类推。