在C中什么是size_t ?

时间:2021-05-14 16:10:54

I am getting confused with size_t in C. I know that it is returned by the sizeof operator. But what exactly is it? Is it a data type?

我在c中与size_t混淆了,我知道它是由sizeof运算符返回的。但究竟是什么呢?它是一种数据类型吗?

Let's say I have a for loop:

假设有一个for循环:

for(i = 0; i < some_size; i++)

Should I use int i; or size_t i;?

我应该使用int I;或者我size_t;?

10 个解决方案

#1


336  

From Wikipedia:

从*:

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3).

根据1999年ISO C标准(C99), size_t是一个无符号整数类型,至少16位(见第7.17和7.18.3节)。

size_tis an unsigned data type defined by several C/C++ standards, e.g. the C99 ISO/IEC 9899 standard, that is defined in stddef.h.1 It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h.

size_tis是由几个C/ c++标准定义的无符号数据类型,例如在stddef.h中定义的C99 ISO/IEC 9899标准。可以通过包含stdlib进一步导入。h作为这个文件的内部子包括stddef.h。

This type is used to represent the size of an object. Library functions that take or return sizes expect them to be of type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a constant value that is compatible with size_t.

该类型用于表示对象的大小。获取或返回大小的库函数期望它们具有类型或具有size_t的返回类型。此外,最常用的基于编译器的运算符sizeof应该评估一个与size_t兼容的常量值。

As an implication, size_t is a type guaranteed to hold any array index.

作为一种暗示,size_t是一种保证持有任何数组索引的类型。

#2


165  

size_t is an unsigned type. So, it cannot represent any negative values(<0). You use it when you are counting something, and are sure that it cannot be negative. For example, strlen() returns a size_t because the length of a string has to be at least 0.

size_t是一个无符号类型。因此,它不能表示任何负值(<0)。当你在计算某样东西时,你使用它,并且确信它不可能是负数。例如,strlen()返回一个size_t,因为字符串的长度必须至少为0。

In your example, if your loop index is going to be always greater than 0, it might make sense to use size_t, or any other unsigned data type.

在您的示例中,如果您的循环索引总是大于0,那么使用size_t或任何其他未签名的数据类型可能是有意义的。

When you use a size_t object, you have to make sure that in all the contexts it is used, including arithmetic, you want non-negative values. For example, let's say you have:

当使用size_t对象时,必须确保在使用的所有上下文(包括算术)中,都需要非负的值。例如,假设你有:

size_t s1 = strlen(str1);
size_t s2 = strlen(str2);

and you want to find the difference of the lengths of str2 and str1. You cannot do:

你要找出str2和str1的长度的差。你不能做的:

int diff = s2 - s1; /* bad */

This is because the value assigned to diff is always going to be a positive number, even when s2 < s1, because the calculation is done with unsigned types. In this case, depending upon what your use case is, you might be better off using int (or long long) for s1 and s2.

这是因为分配给diff的值始终是正数,即使是s2 < s1,因为计算是用无符号类型完成的。在这种情况下,取决于您的用例是什么,您最好在s1和s2中使用int(或long long)。

There are some functions in C/POSIX that could/should use size_t, but don't because of historical reasons. For example, the second parameter to fgets should ideally be size_t, but is int.

在C/POSIX中有一些可以/应该使用size_t的函数,但是不要因为历史原因。例如,fgets的第二个参数应该是size_t,但是是int。

#3


55  

size_t is a type that can hold any array index.

size_t是一个可以容纳任何数组索引的类型。

Depending on the implementation, it can be any of:

根据实施情况,可以是:

unsigned char

无符号字符

unsigned short

无符号短

unsigned int

无符号整型

unsigned long

无符号长

unsigned long long

无符号长时间长

Here's how size_t is defined in stddef.h of my machine:

这里是在stddef中定义size_t的方法。我的机器的h:

typedef unsigned long size_t;

#4


40  

If you are the empirical type

如果你是经验型的。

echo | gcc -E -xc -include 'stddef.h' - | grep size_t

Output for Ubuntu 14.04 64-bit GCC 4.8:

Ubuntu 14.04 64位的输出:

typedef long unsigned int size_t;

Note that stddef.h is provided by GCC and not glibc under src/gcc/ginclude/stddef.h in GCC 4.2.

注意,stddef。h是由GCC提供的,而不是在src/ GCC /ginclude/stddef下的glibc。在GCC 4.2 h。

Interesting C99 appearances

有趣的C99露面

  • malloc takes size_t as argument, so it determines the maximum size that may be allocated.

    malloc将size_t作为参数,因此它决定了可能分配的最大大小。

    And since it is also returned by sizeof, I think it limits the maximum size of a any array.

    由于它也是由sizeof返回的,我认为它限制了任何数组的最大大小。

    See also: The maximum size of an array in C

    参见:C中数组的最大大小。

#5


16  

The manpage for types.h says:

从类型。h说:

size_t shall be an unsigned integer type

size_t应该是无符号整数类型。

#6


10  

Since nobody has yet mentioned it, the primary linguistic significance of size_t is that the sizeof operator returns a value of that type. Likewise, the primary significance of ptrdiff_t is that subtracting one pointer from another will yield a value of that type. Library functions that accept it do so because it will allow such functions to work with objects whose size exceeds UINT_MAX on systems where such objects could exist, without forcing callers to waste code passing a value larger than "unsigned int" on systems where the larger type would suffice for all possible objects.

由于没有人提到它,size_t的主要语言意义是,sizeof运算符返回该类型的值。同样,ptrdiff_t的主要意义是,从另一个指针中减去一个指针将产生该类型的值。库函数,接受它这样做,因为它将允许这些函数处理对象的大小超过UINT_MAX系统这样的对象可能存在的地方,没有强制调用者浪费代码传递一个值大于“unsigned int”系统更大的类型为所有可能的对象就足够了。

#7


4  

size_t and int are not interchangeable. For instance on 64-bit Linux size_t is 64-bit in size (i.e. sizeof(void*)) but int is 32-bit.

size_t和int不能互换。例如,对于64位的Linux size_t,它的大小是64位(即void*),但是int是32位的。

Also note that size_t is unsigned. If you need signed version then there is ssize_t on some platforms and it would be more relevant to your example.

还要注意,size_t是未签名的。如果您需要签名版本,那么在一些平台上有ssize_t,它将更与您的示例相关。

As a general rule I would suggest using int for most general cases and only use size_t/ssize_t when there is a specific need for it (with mmap() for example).

一般情况下,我建议在大多数一般情况下使用int,当有特定需要时,只使用size_t/ssize_t(例如mmap())。

#8


3  

In general, if you are starting at 0 and going upward, always use an unsigned type to avoid an overflow taking you into a negative value situation. This is critically important, because if your array bounds happens to be less than the max of your loop, but your loop max happens to be greater than the max of your type, you will wrap around negative and you may experience a segmentation fault (SIGSEGV). So, in general, never use int for a loop starting at 0 and going upwards. Use an unsigned.

一般来说,如果从0开始向上,总是使用无符号类型,以避免溢出将您带入负值情况。这是非常重要的,因为如果你的数组边界刚好小于你的循环的最大值,但是你的循环最大值恰好大于你的类型的最大值,你将会绕着负值,你可能会经历一个分割错误(SIGSEGV)。所以,一般情况下,从0开始,从0开始,一直向上。使用一个无符号。

#9


0  

size_t is unsigned integer data type. On systems using the GNU C Library, this will be unsigned int or unsigned long int. size_t is commonly used for array indexing and loop counting.

size_t是无符号整数数据类型。在使用GNU C库的系统上,这将是无符号的int或unsigned long int。size_t通常用于数组索引和循环计数。

#10


-5  

From my understanding, size_t is an unsigned integer whose bit size is large enough to hold a pointer of the native architecture.

根据我的理解,size_t是一个无符号整数,它的位大小足以容纳一个本机架构的指针。

So:

所以:

sizeof(size_t) >= sizeof(void*)

#1


336  

From Wikipedia:

从*:

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3).

根据1999年ISO C标准(C99), size_t是一个无符号整数类型,至少16位(见第7.17和7.18.3节)。

size_tis an unsigned data type defined by several C/C++ standards, e.g. the C99 ISO/IEC 9899 standard, that is defined in stddef.h.1 It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h.

size_tis是由几个C/ c++标准定义的无符号数据类型,例如在stddef.h中定义的C99 ISO/IEC 9899标准。可以通过包含stdlib进一步导入。h作为这个文件的内部子包括stddef.h。

This type is used to represent the size of an object. Library functions that take or return sizes expect them to be of type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a constant value that is compatible with size_t.

该类型用于表示对象的大小。获取或返回大小的库函数期望它们具有类型或具有size_t的返回类型。此外,最常用的基于编译器的运算符sizeof应该评估一个与size_t兼容的常量值。

As an implication, size_t is a type guaranteed to hold any array index.

作为一种暗示,size_t是一种保证持有任何数组索引的类型。

#2


165  

size_t is an unsigned type. So, it cannot represent any negative values(<0). You use it when you are counting something, and are sure that it cannot be negative. For example, strlen() returns a size_t because the length of a string has to be at least 0.

size_t是一个无符号类型。因此,它不能表示任何负值(<0)。当你在计算某样东西时,你使用它,并且确信它不可能是负数。例如,strlen()返回一个size_t,因为字符串的长度必须至少为0。

In your example, if your loop index is going to be always greater than 0, it might make sense to use size_t, or any other unsigned data type.

在您的示例中,如果您的循环索引总是大于0,那么使用size_t或任何其他未签名的数据类型可能是有意义的。

When you use a size_t object, you have to make sure that in all the contexts it is used, including arithmetic, you want non-negative values. For example, let's say you have:

当使用size_t对象时,必须确保在使用的所有上下文(包括算术)中,都需要非负的值。例如,假设你有:

size_t s1 = strlen(str1);
size_t s2 = strlen(str2);

and you want to find the difference of the lengths of str2 and str1. You cannot do:

你要找出str2和str1的长度的差。你不能做的:

int diff = s2 - s1; /* bad */

This is because the value assigned to diff is always going to be a positive number, even when s2 < s1, because the calculation is done with unsigned types. In this case, depending upon what your use case is, you might be better off using int (or long long) for s1 and s2.

这是因为分配给diff的值始终是正数,即使是s2 < s1,因为计算是用无符号类型完成的。在这种情况下,取决于您的用例是什么,您最好在s1和s2中使用int(或long long)。

There are some functions in C/POSIX that could/should use size_t, but don't because of historical reasons. For example, the second parameter to fgets should ideally be size_t, but is int.

在C/POSIX中有一些可以/应该使用size_t的函数,但是不要因为历史原因。例如,fgets的第二个参数应该是size_t,但是是int。

#3


55  

size_t is a type that can hold any array index.

size_t是一个可以容纳任何数组索引的类型。

Depending on the implementation, it can be any of:

根据实施情况,可以是:

unsigned char

无符号字符

unsigned short

无符号短

unsigned int

无符号整型

unsigned long

无符号长

unsigned long long

无符号长时间长

Here's how size_t is defined in stddef.h of my machine:

这里是在stddef中定义size_t的方法。我的机器的h:

typedef unsigned long size_t;

#4


40  

If you are the empirical type

如果你是经验型的。

echo | gcc -E -xc -include 'stddef.h' - | grep size_t

Output for Ubuntu 14.04 64-bit GCC 4.8:

Ubuntu 14.04 64位的输出:

typedef long unsigned int size_t;

Note that stddef.h is provided by GCC and not glibc under src/gcc/ginclude/stddef.h in GCC 4.2.

注意,stddef。h是由GCC提供的,而不是在src/ GCC /ginclude/stddef下的glibc。在GCC 4.2 h。

Interesting C99 appearances

有趣的C99露面

  • malloc takes size_t as argument, so it determines the maximum size that may be allocated.

    malloc将size_t作为参数,因此它决定了可能分配的最大大小。

    And since it is also returned by sizeof, I think it limits the maximum size of a any array.

    由于它也是由sizeof返回的,我认为它限制了任何数组的最大大小。

    See also: The maximum size of an array in C

    参见:C中数组的最大大小。

#5


16  

The manpage for types.h says:

从类型。h说:

size_t shall be an unsigned integer type

size_t应该是无符号整数类型。

#6


10  

Since nobody has yet mentioned it, the primary linguistic significance of size_t is that the sizeof operator returns a value of that type. Likewise, the primary significance of ptrdiff_t is that subtracting one pointer from another will yield a value of that type. Library functions that accept it do so because it will allow such functions to work with objects whose size exceeds UINT_MAX on systems where such objects could exist, without forcing callers to waste code passing a value larger than "unsigned int" on systems where the larger type would suffice for all possible objects.

由于没有人提到它,size_t的主要语言意义是,sizeof运算符返回该类型的值。同样,ptrdiff_t的主要意义是,从另一个指针中减去一个指针将产生该类型的值。库函数,接受它这样做,因为它将允许这些函数处理对象的大小超过UINT_MAX系统这样的对象可能存在的地方,没有强制调用者浪费代码传递一个值大于“unsigned int”系统更大的类型为所有可能的对象就足够了。

#7


4  

size_t and int are not interchangeable. For instance on 64-bit Linux size_t is 64-bit in size (i.e. sizeof(void*)) but int is 32-bit.

size_t和int不能互换。例如,对于64位的Linux size_t,它的大小是64位(即void*),但是int是32位的。

Also note that size_t is unsigned. If you need signed version then there is ssize_t on some platforms and it would be more relevant to your example.

还要注意,size_t是未签名的。如果您需要签名版本,那么在一些平台上有ssize_t,它将更与您的示例相关。

As a general rule I would suggest using int for most general cases and only use size_t/ssize_t when there is a specific need for it (with mmap() for example).

一般情况下,我建议在大多数一般情况下使用int,当有特定需要时,只使用size_t/ssize_t(例如mmap())。

#8


3  

In general, if you are starting at 0 and going upward, always use an unsigned type to avoid an overflow taking you into a negative value situation. This is critically important, because if your array bounds happens to be less than the max of your loop, but your loop max happens to be greater than the max of your type, you will wrap around negative and you may experience a segmentation fault (SIGSEGV). So, in general, never use int for a loop starting at 0 and going upwards. Use an unsigned.

一般来说,如果从0开始向上,总是使用无符号类型,以避免溢出将您带入负值情况。这是非常重要的,因为如果你的数组边界刚好小于你的循环的最大值,但是你的循环最大值恰好大于你的类型的最大值,你将会绕着负值,你可能会经历一个分割错误(SIGSEGV)。所以,一般情况下,从0开始,从0开始,一直向上。使用一个无符号。

#9


0  

size_t is unsigned integer data type. On systems using the GNU C Library, this will be unsigned int or unsigned long int. size_t is commonly used for array indexing and loop counting.

size_t是无符号整数数据类型。在使用GNU C库的系统上,这将是无符号的int或unsigned long int。size_t通常用于数组索引和循环计数。

#10


-5  

From my understanding, size_t is an unsigned integer whose bit size is large enough to hold a pointer of the native architecture.

根据我的理解,size_t是一个无符号整数,它的位大小足以容纳一个本机架构的指针。

So:

所以:

sizeof(size_t) >= sizeof(void*)