C++ 和/或 C 中的 size_t 与 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/994288/
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
size_t vs int in C++ and/or C
提问by John Jiang
Why is it that in C++ containers, it returns a size_type
rather than an int
? If we're creating our own structures, should we also be encouraged to use size_type
?
为什么在 C++ 容器中,它返回一个size_type
而不是一个int
?如果我们正在创建自己的结构,是否也应该鼓励我们使用size_type
?
采纳答案by D.Shawley
In general, size_t
should be used whenever you are measuring the size of something. It is really strange that size_t
is only required to represent between 0 and SIZE_MAX
bytes and SIZE_MAX
is only required to be 65,535...
一般来说,size_t
无论何时测量某物的大小,都应该使用它。真的很奇怪,size_t
只需要表示0到SIZE_MAX
字节之间,而且SIZE_MAX
只需要65535……
The other interesting constraints from the C++ and C Standards are:
C++ 和 C 标准中其他有趣的约束是:
- the return type of
sizeof()
issize_t
and it is an unsigned integer operator new()
takes the number of bytes to allocate as asize_t
parametersize_t
is defined in<cstddef>
SIZE_MAX
is defined in<limits.h>
in C99 but not mentioned in C++98?!size_t
is not included in the list of fundamental integer typesso I have always assumed thatsize_t
is a type alias for one of the fundamental types:char
,short int
,int
, andlong int
.
sizeof()
is的返回类型size_t
是一个无符号整数operator new()
将要分配的字节数作为size_t
参数size_t
定义在<cstddef>
SIZE_MAX
<limits.h>
在 C99 中定义但在 C++98 中未提及?!size_t
不包括在名单基本整数类型,所以我一直认为size_t
是基本类型之一的类型别名:char
,short int
,int
,和long int
。
If you are counting bytes, then you should definitely be using size_t
. If you are counting the number of elements, then you should probably use size_t
since this seems to be what C++ has been using. In any case, you don't want to use int
- at the very least use unsigned long
or unsigned long long
if you are using TR1. Or... even better... typedef
whatever you end up using to size_type
or just include <cstddef>
and use std::size_t
.
如果您正在计算字节数,那么您绝对应该使用size_t
. 如果您正在计算元素的数量,那么您可能应该使用,size_t
因为这似乎是 C++ 一直在使用的。在任何情况下,您都不想使用int
- 至少使用unsigned long
或者unsigned long long
如果您正在使用 TR1。或者......甚至更好......typedef
无论你最终使用size_type
或只是包含<cstddef>
和使用std::size_t
.
回答by Dave
A few reasons might be:
可能有以下几个原因:
- The type (size_t) can be defined as the largest unsigned integer on that platform. For example, it might be defined as a 32 bit integer or a 64 bit integer or something else altogether that's capable of storing unsigned values of a great length
- To make it clear when reading a program that the value is a size and not just a "regular" int
- 类型 (size_t) 可以定义为该平台上最大的无符号整数。例如,它可能被定义为一个 32 位整数或一个 64 位整数或其他能够存储很大长度的无符号值的东西
- 在阅读程序时明确表示该值是一个大小而不仅仅是一个“常规”int
If you're writing an app that's just for you and/or throwaway, you're probably fine to use a basic int. If you're writing a library or something substantial, size_t is probably a better way to go.
如果您正在编写一个只适合您和/或一次性的应用程序,您可能可以使用基本的 int。如果你正在编写一个库或一些重要的东西, size_t 可能是一个更好的方法。
回答by Andy Ross
Some of the answers are more complicated than necessary. A size_t is an unsigned integer type that is guaranteed to be big enough to store the size in bytes of any object in memory. In practice, it is always the same size as the pointer type. On 32 bit systems it is 32 bits. On 64 bit systems it is 64 bits.
有些答案比必要的更复杂。size_t 是一种无符号整数类型,它保证足够大以存储内存中任何对象的字节大小。实际上,它始终与指针类型的大小相同。在 32 位系统上,它是 32 位。在 64 位系统上,它是 64 位。
回答by rlbond
All containers in the stl have various typedefs. For example, value_type
is the element type, and size_type
is the number stored type. In this way the containers are completely generic based on platform and implementation.
stl 中的所有容器都有各种类型定义。例如,value_type
是元素类型,size_type
是数字存储类型。通过这种方式,容器是基于平台和实现的完全通用的。
If you are creating your own containers, you should use size_type
too. Typically this is done
如果您要创建自己的容器,也应该使用size_type
。通常这样做
typedef std::size_t size_type;
If you want a container's size, you should write
如果你想要一个容器的大小,你应该写
typedef vector<int> ints;
ints v;
v.push_back(4);
ints::size_type s = v.size();
What's nice is that if later you want to use a list, just change the typedef to
好的是,如果以后你想使用一个列表,只需将 typedef 更改为
typedef list<int> ints;
And it will still work!
它仍然会起作用!
回答by MatrixFrog
I assume you mean "size_t" -- this is a way of indicating an unsigned integer (an integer that can only be positive, never negative) -- it makes sense for containers' sizes since you can't have an array with a size of -7. I wouldn't say that you have to use size_t but it does indicate to others using your code "This number here is always positive." It also gives you a greater range of positive numbers, but that is likely to be unimportant unless you have some very big containers.
我假设你的意思是“size_t”——这是一种表示无符号整数(一个只能为正数,不能为负数的整数)的方式——它对容器的大小有意义,因为你不能拥有一个大小的数组-7。我不会说您必须使用 size_t ,但它确实向使用您的代码的其他人表明“这里的数字始终为正数。” 它还为您提供了更大范围的正数,但这可能并不重要,除非您有一些非常大的容器。
回答by Eugene Yokota
C++ is a language that could be implemented on different hardware architectures and platforms. As time has gone by it has supported 16-, 32-, and 64-bit architecture, and likely others in the future. size_type
and other type aliases are ways for libraries to insulate the programmers/code from implementation details.
C++ 是一种可以在不同硬件架构和平台上实现的语言。随着时间的推移,它已支持 16 位、32 位和 64 位架构,未来可能还会支持其他架构。size_type
和其他类型别名是库将程序员/代码与实现细节隔离的方式。
Assuming the size_type
uses 32 bits on 32-bit machines and 64 bits on 64-bit machines, the same source code likely would work better if you've used size_type where needed. In most cases you could assume it would be the same as unsigned int
, but it's not guaranteed.
假设size_type
在 32 位机器上使用 32 位,在 64 位机器上使用 64 位,如果您在需要的地方使用 size_type,则相同的源代码可能会更好地工作。在大多数情况下,您可以假设它与 相同unsigned int
,但不能保证。
size_type
is used to express capacities of STL containers like std::vector
whereas size_t
is used to express byte size of an object in C/C++.
size_type
用于表达像STL容器的容量std::vector
而size_t
用于表达在C / C的对象的++字节大小。
回答by Charles Ma
ints are not guaranteed to be 4 bytes in the specification, so they are not reliable. Yes, size_type would be preferred over ints
规范中不保证整数为 4 个字节,因此它们不可靠。是的,size_type 比整数更受欢迎
回答by Dan Olson
size_t
is unsigned, so even if they're both 32 bits it doesn't mean quite the same thing as an unqualified int. I'm not sure why they added the type, but on many platforms today sizeof (size_t) == sizeof (int) == sizeof (long)
, so which type you choose is up to you. Note that those relations aren't guaranteed by the standard and are rapidly becoming out of date as 64 bit platforms move in.
size_t
是无符号的,因此即使它们都是 32 位,也与不合格的 int 的含义并不完全相同。我不知道他们为什么要添加类型,但是今天在许多平台上sizeof (size_t) == sizeof (int) == sizeof (long)
,您选择哪种类型取决于您。请注意,标准不能保证这些关系,并且随着 64 位平台的进入,这些关系很快就会过时。
For your own code, if you need to represent something that is a "size" conceptually and can never be negative, size_t
would be a fine choice.
对于您自己的代码,如果您需要在概念上表示某种“大小”并且永远不会是负数,size_t
这将是一个不错的选择。
回答by Shing Yip
void f1(size_t n) {
if (n <= myVector.size()) { assert(false); }
size_t n1 = n - myVector.size(); // bug! myVector.size() can be > n
do_stuff_n_times(n1);
}
void f2(int n) {
int n1 = n - static_cast<int>(myVector.size());
assert(n1 >= 0);
do_stuff_n_times(n1);
}
f1() and f2() both have the same bug, but detecting the problem in f2() is easier. For more complex code, unsigned integer arithmetic bugs are not as easy to identify.
f1() 和 f2() 都有相同的错误,但在 f2() 中检测问题更容易。对于更复杂的代码,无符号整数算术错误并不容易识别。
Personally I use signed int for all my sizes unless unsigned int should be used. I have never run into situation where my size won't fit into a 32 bit signed integer. I will probably use 64 bit signed integers before I use unsigned 32 bit integers.
我个人对所有尺寸都使用 signed int ,除非应该使用 unsigned int 。我从来没有遇到过我的大小不适合 32 位有符号整数的情况。在使用无符号 32 位整数之前,我可能会使用 64 位有符号整数。
The problem with using signed integers for size is a lot of static_cast
from size_t
to int
in your code.
用符号整数size的问题是很多的static_cast
,从size_t
到int
在你的代码。