Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

c++ - C ++标准说明int,long类型的大小是什么?(What does the C++ standard state the size of int, long type to be?)

I'm looking for detailed information regarding the size of basic C++ types.

(我正在寻找有关基本C ++类型大小的详细信息。)

I know that it depends on the architecture (16 bits, 32 bits, 64 bits) and the compiler.

(我知道这取决于体系结构(16位,32位,64位)和编译器。)

But are there any standards for C++?

(但是C ++有什么标准吗?)

I'm using Visual Studio 2008 on a 32-bit architecture.

(我在32位体系结构上使用Visual Studio 2008。)

Here is what I get:

(这是我得到的:)

char  : 1 byte
short : 2 bytes
int   : 4 bytes
long  : 4 bytes
float : 4 bytes
double: 8 bytes

I tried to find, without much success, reliable information stating the sizes of char , short , int , long , double , float (and other types I didn't think of) under different architectures and compilers.

(我试图找到没有可靠信息的可靠信息,这些信息说明了不同体系结构和编译器下charshortintlongdoublefloat (以及其他我没有想到的类型)的大小。)

  ask by Jér?me translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The C++ standard does not specify the size of integral types in bytes, but it specifies minimum ranges they must be able to hold.

(C ++标准未指定整数类型的大小(以字节为单位),但指定了它们必须能够容纳的最小范围。)

You can infer minimum size in bits from the required range.

(您可以从所需范围以位为单位推断最小大小。)

You can infer minimum size in bytes from that and the value of the CHAR_BIT macro that defines the number of bits in a byte .

(您可以从中推断出最小大小(以字节为单位),并可以定义定义字节中位数CHAR_BIT宏的值。)

In all but the most obscure platforms it's 8, and it can't be less than 8. That's because it must be large enough to hold "the eight-bit code units of the Unicode UTF-8 encoding form."

(在除最晦涩的平台之外的所有平台上,它都是8,并且不能小于8。这是因为它必须足够大,以容纳“ Unicode UTF-8编码形式的八位代码单元”。)

One additional constraint for char is that its size is always 1 byte, or CHAR_BIT bits (hence the name).

(char另一个约束是其大小始终为1个字节,即CHAR_BIT位(因此而得名)。)

This is stated explicitly in the standard.

(这在标准中有明确规定。)

The C standard is a normative reference for the C++ standard, so even though it doesn't state these requirements explicitly, C++ requires the minimum ranges required by the C standard (page 22), which are the same as those from Data Type Ranges on MSDN :

(C标准是C ++标准的规范性参考 ,因此,即使未明确说明这些要求,C ++仍需要C标准所要求的最小范围(第22页),该范围与数据类型范围中的相同。 MSDN :)

  1. signed char : -127 to 127 (note, not -128 to 127; this accommodates 1's-complement and sign-and-magnitude platforms)

    (signed char :-127至127(注意,不是-128至127;这可容纳1的补码和符号幅度平台))

  2. unsigned char : 0 to 255

    (unsigned char :0到255)

  3. "plain" char : same range as signed char or unsigned char , implementation-defined

    (“普通” char :与已signed char或未unsigned char相同的范围, 实现定义)

  4. signed short : -32767 to 32767

    (signed short字母:-32767至32767)

  5. unsigned short : 0 to 65535

    (unsigned short :0到65535)

  6. signed int : -32767 to 32767

    (signed int :-32767至32767)

  7. unsigned int : 0 to 65535

    (unsigned int :0到65535)

  8. signed long : -2147483647 to 2147483647

    (signed long :-2147483647至2147483647)

  9. unsigned long : 0 to 4294967295

    (unsigned long :0到4294967295)

  10. signed long long : -9223372036854775807 to 9223372036854775807

    (signed long long :-9223372036854775807至9223372036854775807)

  11. unsigned long long : 0 to 18446744073709551615

    (unsigned long long :0到18446744073709551615)

A C++ (or C) implementation can define the size of a type in bytes sizeof(type) to any value, as long as

(C ++(或C)实现可以将类型的sizeof(type)以字节为单位) sizeof(type)为任何值,只要)

  1. the expression sizeof(type) * CHAR_BIT evaluates to a number of bits high enough to contain required ranges, and

    (表达式sizeof(type) * CHAR_BIT计算得出的位数足以包含所需的范围,并且)

  2. the ordering of type is still valid (eg sizeof(int) <= sizeof(long) ).

    (类型的顺序仍然有效(例如, sizeof(int) <= sizeof(long) )。)

Putting this all together, we are guaranteed that:

(综合所有这些,我们保证:)

  • char , signed char , and unsigned char are at least 8 bits

    (charsigned charunsigned char至少为8位)

  • signed short , unsigned short , signed int , and unsigned int are at least 16 bits

    (signed shortunsigned short ,有signed intunsigned int至少为16位)

  • signed long and unsigned long are at least 32 bits

    (signed longunsigned long至少为32位)

  • signed long long and unsigned long long are at least 64 bits

    (signed long long unsigned long longunsigned long long至少为64位)

No guarantee is made about the size of float or double except that double provides at least as much precision as float .

(不保证floatdouble的大小,但double至少提供与float一样的精度。)

The actual implementation-specific ranges can be found in <limits.h> header in C, or <climits> in C++ (or even better, templated std::numeric_limits in <limits> header).

(特定于实现的实际范围可以在C的<limits.h>标头中找到,或在C ++的<climits>中找到(甚至更好的是,在<limits>标头中的模板化std::numeric_limits )。)

For example, this is how you will find maximum range for int :

(例如,这是您将找到int最大范围的方法:)

C:

(C:)

#include <limits.h>
const int min_int = INT_MIN;
const int max_int = INT_MAX;

C++ :

(C ++ :)

#include <limits>
const int min_int = std::numeric_limits<int>::min();
const int max_int = std::numeric_limits<int>::max();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...