size_t和无符号int之间的区别?

时间:2022-11-13 16:09:14

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

我对size_t很困惑。我在网上搜索过,发现size_t是无符号类型,所以它可以表示非负值。

My first question is that if it is uses to represent non negative values, why don't we use unsigned int instead of size_t?

我的第一个问题是,如果它是用来表示非负值的,为什么我们不使用无符号int而不是size_t呢?

My second question is: are size_t and unsigned int interchangeable or not? If not then why?

我的第二个问题是:size_t和unsigned int是否可以互换?如果不是为什么?

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

有没有人能给我一个关于size_t和它的简短工作的好例子?

6 个解决方案

#1


49  

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

如果它用来表示非负值,那么为什么我们不用无符号int代替size_t

Because unsigned int is not the only unsigned integral type. size_t could be any of unsigned char, unsigned short, unsigned int, unsigned long or unsigned long long, depending on the implementation.

因为无符号整型不是唯一的无符号整型。size_t可以是任意的无符号字符、无符号短、无符号整型、无符号长或无符号长,这取决于实现。

Second question is that size_t and unsinged int are interchangable or not and if not then why?

第二个问题是size_t和unsinged 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_t when you are describing the size of an object. In particular, the sizeof operator and various standard library functions, such as strlen(), return size_t.

我不太明白你所说的“短暂的工作”是什么意思。它的工作方式与任何其他未签名的类型(特别是,它的类型一样)。我们鼓励您在描述对象的大小时使用size_t。特别是sizeof运算符和各种标准库函数,如strlen(),返回size_t。

Bonus: here's a good article (article deleted, use wayback machine snapshot) about size_t (and the closely related ptrdiff_t type). It reasons very well why you should use it.

附注:这里有一篇关于size_t(以及与之密切相关的ptrdiff_t类型)的好文章(文章被删除,使用回路机快照)。它很好地解释了为什么你应该使用它。

#2


36  

There are 5 standard unsigned integer types in C:

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

  • 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_t is 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 sizeof operator.

size_t是一个typedef(例如。对于一些未签名的类型(可能是上面的一个,但可能是一个扩展的无符号整数类型,尽管这是不可能的)。这是sizeof运算符给出的类型。

On one system, it might make sense to use unsigned int to represent sizes; on another, it might make more sense to use unsigned long or unsigned long long. (size_t is unlikely to be either unsigned char or unsigned short, but that's permitted).

在一个系统上,使用无符号int表示大小是有意义的;另一方面,使用无符号long或无符号long可能更有意义。(size_t不太可能是无符号char还是无符号short,但这是允许的)。

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

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

Code that assumes sizeof yields an unsigned int would not be portable. Code that assumes it yields a size_t is more likely to be portable.

假设sizeof产生无符号整数的代码是不可移植的。假设生成size_t的代码更可能是可移植的。

#3


7  

size_t has 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 int because the size of int is specified by the data model. For example LLP64 uses a 32-bit int and ILP64 uses a 64-bit int.

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

#4


3  

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实现可以创建的任何数据对象的大小。这个数据类型可能更小(在比特数中),大的或完全相同的无符号整数。

#5


3  

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位的系统中取1 64位。换句话说,size_t类型的变量可以安全地存储指针。例外是指向类函数的指针,但这是一种特殊情况。尽管size_t可以存储一个指针,但是最好为此使用另一个无符号整数类型uintptr_t(其名称反映其功能)。size_t和uintptr_t类型是同义词。size_t类型通常用于循环计数器、数组索引和地址算法。size_t类型的最大可能值是常量SIZE_MAX。

#6


2  

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

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

#1


49  

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

如果它用来表示非负值,那么为什么我们不用无符号int代替size_t

Because unsigned int is not the only unsigned integral type. size_t could be any of unsigned char, unsigned short, unsigned int, unsigned long or unsigned long long, depending on the implementation.

因为无符号整型不是唯一的无符号整型。size_t可以是任意的无符号字符、无符号短、无符号整型、无符号长或无符号长,这取决于实现。

Second question is that size_t and unsinged int are interchangable or not and if not then why?

第二个问题是size_t和unsinged 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_t when you are describing the size of an object. In particular, the sizeof operator and various standard library functions, such as strlen(), return size_t.

我不太明白你所说的“短暂的工作”是什么意思。它的工作方式与任何其他未签名的类型(特别是,它的类型一样)。我们鼓励您在描述对象的大小时使用size_t。特别是sizeof运算符和各种标准库函数,如strlen(),返回size_t。

Bonus: here's a good article (article deleted, use wayback machine snapshot) about size_t (and the closely related ptrdiff_t type). It reasons very well why you should use it.

附注:这里有一篇关于size_t(以及与之密切相关的ptrdiff_t类型)的好文章(文章被删除,使用回路机快照)。它很好地解释了为什么你应该使用它。

#2


36  

There are 5 standard unsigned integer types in C:

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

  • 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_t is 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 sizeof operator.

size_t是一个typedef(例如。对于一些未签名的类型(可能是上面的一个,但可能是一个扩展的无符号整数类型,尽管这是不可能的)。这是sizeof运算符给出的类型。

On one system, it might make sense to use unsigned int to represent sizes; on another, it might make more sense to use unsigned long or unsigned long long. (size_t is unlikely to be either unsigned char or unsigned short, but that's permitted).

在一个系统上,使用无符号int表示大小是有意义的;另一方面,使用无符号long或无符号long可能更有意义。(size_t不太可能是无符号char还是无符号short,但这是允许的)。

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

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

Code that assumes sizeof yields an unsigned int would not be portable. Code that assumes it yields a size_t is more likely to be portable.

假设sizeof产生无符号整数的代码是不可移植的。假设生成size_t的代码更可能是可移植的。

#3


7  

size_t has 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 int because the size of int is specified by the data model. For example LLP64 uses a 32-bit int and ILP64 uses a 64-bit int.

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

#4


3  

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实现可以创建的任何数据对象的大小。这个数据类型可能更小(在比特数中),大的或完全相同的无符号整数。

#5


3  

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位的系统中取1 64位。换句话说,size_t类型的变量可以安全地存储指针。例外是指向类函数的指针,但这是一种特殊情况。尽管size_t可以存储一个指针,但是最好为此使用另一个无符号整数类型uintptr_t(其名称反映其功能)。size_t和uintptr_t类型是同义词。size_t类型通常用于循环计数器、数组索引和地址算法。size_t类型的最大可能值是常量SIZE_MAX。

#6


2  

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

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