C++ 中的 size_t 和 int 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/502856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:42:16  来源:igfitidea点击:

What's the difference between size_t and int in C++?

c++ctypesint

提问by tunnuz

In several C++ examples I see a use of the type size_twhere I would have used a simple int. What's the difference, and why size_tshould be better?

在几个 C++ 示例中,我看到使用了size_t我会使用简单int. 有什么区别,为什么size_t应该更好?

采纳答案by Joao da Silva

From the friendly Wikipedia:

来自友好的维基百科

The stdlib.h and stddef.h header files define a datatype called size_twhich is used to represent the size of an object. Library functions that take sizes expect them to be of type size_t, and the sizeof operator evaluates to size_t.

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors, particularly as 64-bit architectures become more prevalent.

stdlib.h 和 stddef.h 头文件定义了一个名为size_t的数据类型,用于表示对象的大小。采用大小的库函数期望它们的类型为 size_t,而 sizeof 运算符的计算结果为 size_t。

size_t 的实际类型是平台相关的;一个常见的错误是假设 size_t 与 unsigned int 相同,这会导致编程错误,尤其是在 64 位体系结构变得更加普遍时。

Also, check Why size_t matters

另外,检查为什么 size_t 很重要

回答by Axelle Ziegler

size_t is the type used to represent sizes (as its names implies). Its platform (and even potentially implementation) dependent, and should be used only for this purpose. Obviously, representing a size, size_t is unsigned. Many stdlib functions, including malloc, sizeof and various string operation functions use size_t as a datatype.

size_t 是用于表示大小的类型(顾名思义)。它的平台(甚至可能的实现)依赖,并且应该仅用于此目的。显然,代表一个大小,size_t 是无符号的。许多 stdlib 函数,包括 malloc、sizeof 和各种字符串操作函数都使用 size_t 作为数据类型。

An int is signed by default, and even though its size is also platform dependant, it will be a fixed 32bits on most modern machine (and though size_t is 64 bits on 64-bits architecture, int remain 32bits long on those architectures).

默认情况下,int 是有符号的,即使它的大小也取决于平台,但在大多数现代机器上它将是固定的 32 位(虽然 size_t 在 64 位架构上是 64 位,但 int 在这些架构上保持 32 位长)。

To summarize : use size_t to represent the size of an object and int (or long) in other cases.

总结一下:使用 size_t 表示对象的大小,其他情况下使用 int(或 long)。

回答by Davislor

The size_ttype is defined as the unsigned integral type of the sizeofoperator. In the real world, you will often see intdefined as 32 bits (for backward compatibility) but size_tdefined as 64 bits (so you can declare arrays and structures more than 4 GiB in size) on 64-bit platforms. If a long intis also 64-bits, this is called the LP64 convention; if long intis 32 bits but long long intand pointers are 64 bits, that's LLP64. You also might get the reverse, a program that uses 64-bit instructions for speed, but 32-bit pointers to save memory. Also, intis signed and size_tis unsigned.

size_t类型被定义为无符号整数类型的的sizeof操作者。在现实世界中,您经常会看到在 64 位平台上int定义为 32 位(为了向后兼容)但size_t定义为 64 位(因此您可以声明大小超过 4 GiB 的数组和结构)。如果 along int也是 64 位,这称为 LP64 约定;如果long int是 32 位但long long int指针是 64 位,那就是 LLP64。您也可能得到相反的结果,该程序使用 64 位指令来提高速度,但使用 32 位指针来节省内存。此外,int已签名size_t且未签名。

There were historically a number of other platforms where addresses were wider or shorter than the native size of int. In fact, in the '70s and early '80s, this was more common than not: all the popular 8-bit microcomputers had 8-bit registers and 16-bit addresses, and the transition between 16 and 32 bits also produced many machines that had addresses wider than their registers. I occasionally still see questions here about Borland Turbo C for MS-DOS, whose Huge memory mode had 20-bit addresses stored in 32 bits on a 16-bit CPU (but which could support the 32-bit instruction set of the 80386); the Motorola 68000 had a 16-bit ALU with 32-bit registers and addresses; there were IBM mainframes with 15-bit, 24-bit or 31-bit addresses. You also still see different ALU and address-bus sizes in embedded systems.

历史上有许多其他平台的地址比int. 事实上,在 70 年代和 80 年代初期,这种情况比较普遍:所有流行的 8 位微机都有 8 位寄存器和 16 位地址,16 位和 32 位之间的过渡也产生了许多机器有比他们的寄存器更宽的地址。我偶尔仍然在这里看到有关 MS-DOS 的 Borland Turbo C 的问题,它的巨大内存模式在 16 位 CPU 上以 32 位存储了 20 位地址(但可以支持 80386 的 32 位指令集);Motorola 68000 有一个带有 32 位寄存器和地址的 16 位 ALU;有具有 15 位、24 位或 31 位地址的 IBM 大型机。您还可以在嵌入式系统中看到不同的 ALU 和地址总线大小。

Any time intis smaller than size_t, and you try to store the size or offset of a very large file or object in an unsigned int, there is the possibility that it could overflow and cause a bug. With an int, there is also the possibility of getting a negative number. If an intor unsigned intis wider, the program will run correctly but waste memory.

任何时间int小于size_t,并且您尝试将非常大的文件或对象的大小或偏移量存储在 中unsigned int,它都有可能溢出并导致错误。使用int,也有可能得到负数。如果 anintunsigned int更宽,程序将正确运行但会浪费内存。

You should generally use the correct type for the purpose if you want portability. A lot of people will recommend that you use signed math instead of unsigned (to avoid nasty, subtle bugs like 1U < -3). For that purpose, the standard library defines ptrdiff_tin <stddef.h>as the signed type of the result of subtracting a pointer from another.

如果您想要可移植性,您通常应该为此目的使用正确的类型。很多人会建议您使用有符号数学而不是无符号数学(以避免诸如 之类的令人讨厌的微妙错误1U < -3)。为此,标准库将ptrdiff_tin定义<stddef.h>为从另一个指针中减去一个指针的结果的有符号类型。

That said, a workaround might be to bounds-check all addresses and offsets against INT_MAXand either 0or INT_MINas appropriate, and turn on the compiler warnings about comparing signed and unsigned quantities in case you miss any. You should always, always, always be checking your array accesses for overflow in C anyway.

也就是说,一种解决方法可能是对所有地址和偏移量进行边界检查INT_MAX0或者INT_MIN在适当的情况下,并打开编译器关于比较有符号和无符号数量的警告,以防你错过任何一个。无论如何,您应该始终,始终,始终检查数组访问是否存在 C 溢出。

回答by graham.reeds

It's because size_t can be anything other than an int (maybe a struct). The idea is that it decouples it's job from the underlying type.

这是因为 size_t 可以是 int (可能是结构)以外的任何东西。这个想法是它将它的工作与底层类型分离。

回答by sundar

The definition of SIZE_Tis found at: https://msdn.microsoft.com/en-us/library/cc441980.aspxand https://msdn.microsoft.com/en-us/library/cc230394.aspx

的定义SIZE_T位于:https: //msdn.microsoft.com/en-us/library/cc441980.aspxhttps://msdn.microsoft.com/en-us/library/cc230394.aspx

Pasting here the required information:

在此处粘贴所需信息:

SIZE_Tis a ULONG_PTRrepresenting the maximum number of bytes to which a pointer can point.

SIZE_TULONG_PTR表示指针可以指向的最大字节数。

This type is declared as follows:

该类型声明如下:

typedef ULONG_PTR SIZE_T;

A ULONG_PTRis an unsigned long type used for pointer precision. It is used when casting a pointer to a long type to perform pointer arithmetic.

AULONG_PTR是用于指针精度的 unsigned long 类型。它用于将指针转换为 long 类型以执行指针运算。

This type is declared as follows:

该类型声明如下:

typedef unsigned __int3264 ULONG_PTR;