C语言 size_t 和 unsigned int 之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19732319/
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-09-02 07:52:02  来源:igfitidea点击:

Difference between size_t and unsigned int?

ctypesunsigned

提问by Vikas Verma

I am so confused about size_t. I have searched on the internet and everywhere mentioned that size_tis an unsigned type so, it can represent only non-negative values.

我很困惑size_t。我在互联网上搜索过,到处都提到它size_t是无符号类型,因此它只能表示非负值。

My first question is: if it is used to represent only non-negative values, why don't we use unsigned intinstead of size_t?

我的第一个问题是:如果它仅用于表示非负值,为什么我们不使用unsigned int代替size_t

My second question is: are size_tand unsigned intinterchangeable or not? If not, then why?

我的第二个问题是:size_tunsigned int互换或不?如果不是,那为什么呢?

And can anyone give me a good example of size_tand briefly its workings?

谁能给我一个很好的例子size_t并简要介绍它的工作原理?

采纳答案by Vikas Verma

if it is use to represent non negative value so why we not using unsigned intinstead of size_t

如果它用于表示非负值,那么为什么我们不使用unsigned int代替size_t

Because unsigned intis not the only unsigned integer type. size_tcould be any of unsigned char, unsigned short, unsigned int, unsigned longor unsigned long long, depending on the implementation.

因为unsigned int不是唯一的无符号整数类型。size_t可以是、、或 中的任何一个unsigned char,具体取决于实现。unsigned shortunsigned intunsigned longunsigned long long

Second question is that size_tand unsigned intare interchangeable or not and if not then why?

第二个问题是size_tunsigned int是否可以互换,如果不能,那么为什么?

They aren't interchangeable, for the reason explained above ^^.

由于上述原因,它们不可互换^^

And can anyone give me a good example of size_t and its brief working ?

谁能给我一个 size_t 及其简短工作的好例子?

I don't quite get what you mean by "its brief working". It works like any other unsigned type (in particular, like the type it's typedeffed to). You are encouraged to use size_twhen you are describing the size of an object. In particular, the sizeofoperator and various standard library functions, such as strlen(), return size_t.

我不太明白你所说的“简短的工作”是什么意思。它像任何其他无符号类型一样工作(特别是像它被类型化的类型)。size_t当您描述对象的大小时,鼓励您使用。特别是sizeof运算符和各种标准库函数,例如strlen()、 return size_t

Bonus: here's a good articleabout size_t(and the closely related ptrdiff_ttype). It reasons very well why you should use it.

奖励:这是一篇关于size_t(以及密切相关的ptrdiff_t类型)的好文章。这很好地说明了为什么您应该使用它。

回答by Keith Thompson

There are 5 standard unsigned integer types in C:

C 中有 5 种标准的无符号整数类型:

  • unsigned char
  • unsigned short
  • unsigned int
  • unsigned long
  • unsigned long long
  • unsigned char
  • unsigned short
  • unsigned int
  • unsigned long
  • unsigned long long

with various requirements for their sizes and ranges (briefly, each type's range is a subset of the next type's range, but some of them may have the same range).

对其大小和范围有各种要求(简而言之,每种类型的范围都是下一种类型范围的子集,但其中一些可能具有相同的范围)。

size_tis a typedef(i.e., an alias) for some unsigned type, (probably one of the above but possibly an extended unsigned integer type, though that's unlikely). It's the type yielded by the sizeofoperator.

size_ttypedef某个无符号类型的(即别名)(可能是上述类型之一,但可能是扩展的无符号整数类型,尽管这不太可能)。它是sizeof运算符产生的类型。

On one system, it might make sense to use unsigned intto represent sizes; on another, it might make more sense to use unsigned longor unsigned long long. (size_tis unlikely to be either unsigned charor unsigned short, but that's permitted).

在一个系统上,使用unsigned int来表示大小可能是有意义的;另一方面,使用unsigned longor可能更有意义unsigned long long。(size_t不太可能是unsigned charunsigned short,但这是允许的)。

The purpose of size_tis to relieve the programmer from having to worry about whichof the predefined types is used to represent sizes.

的目的size_t是让程序员不必担心使用哪种预定义类型来表示大小。

Code that assumes sizeofyields an unsigned intwould not be portable. Code that assumes it yields a size_tis more likely to be portable.

假定sizeof产生 a 的代码unsigned int不可移植。假设它产生 a 的代码size_t更有可能是可移植的。

回答by DrYap

size_thas a specific restriction.

size_t有特定的限制。

Quoting from http://www.cplusplus.com/reference/cstring/size_t/:

引自http://www.cplusplus.com/reference/cstring/size_t/

Alias of one of the fundamental unsigned integer types.

It is a type able to represent the size of any object in bytes: size_t is the type returned by the sizeof operator and is widely used in the standard library to represent sizes and counts.

基本无符号整数类型之一的别名。

它是一种能够以字节为单位表示任何对象大小的类型:size_t 是 sizeof 运算符返回的类型,在标准库中广泛用于表示大小和计数。

It is not interchangeable with unsigned intbecause the size of intis specified by the data model. For example LLP64 uses a 32-bit intand ILP64 uses a 64-bit int.

它不能与 互换,unsigned int因为 的大小int由数据模型指定。例如 LLP64 使用 32 位int,ILP64 使用 64 位int.

回答by Thomas Padron-McCarthy

size_t is used to store sizes of data objects, and is guaranteed to be able to hold the size of any data object that the particular C implementation can create. This data type may be smaller (in number of bits), bigger or exactly the same as unsigned int.

size_t 用于存储数据对象的大小,并保证能够保存特定 C 实现可以创建的任何数据对象的大小。此数据类型可能更小(以位数计)、更大或与 unsigned int 完全相同。

回答by Ed Heal

Apart from the other answers it also documents the code and tells people that you are talking about size of objects in memory

除了其他答案之外,它还记录了代码并告诉人们您正在谈论内存中对象的大小

回答by user2873459

size_t type is a base unsigned integer type of C/C++ language. It is the type of the result returned by sizeof operator. The type's size is chosen so that it could store the maximum size of a theoretically possible array of any type. On a 32-bit system size_t will take 32 bits, on a 64- bit one 64 bits. In other words, a variable of size_t type can safely store a pointer. The exception is pointers to class functions but this is a special case. Although size_t can store a pointer, it is better to use another unsigned integer type uintptr_t for that purpose (its name reflects its capability). The types size_t and uintptr_t are synonyms. size_t type is usually used for loop counters, array indexing and address arithmetic. The maximum possible value of size_t type is constant SIZE_MAX .

size_t 类型是 C/C++ 语言的基本无符号整数类型。它是 sizeof 运算符返回的结果的类型。选择类型的大小以便它可以存储任何类型的理论上可能的数组的最大大小。在 32 位系统上 size_t 将占用 32 位,在 64 位系统上为 64 位。换句话说,size_t 类型的变量可以安全地存储指针。例外是指向类函数的指针,但这是一个特例。尽管 size_t 可以存储指针,但最好为此目的使用另一个无符号整数类型 uintptr_t(其名称反映了其功能)。类型 size_t 和 uintptr_t 是同义词。size_t 类型通常用于循环计数器、数组索引和地址算法。size_t 类型的最大可能值是常量 SIZE_MAX 。

回答by Kaushal Billore

In simple words size_t is platform and as well as implementation dependent whereas unsigned int is only platform dependent.

简单来说,size_t 是平台和实现相关的,而 unsigned int 仅是平台相关的。