我和c中的size_t搞混了,我知道它是由sizeof操作符返回的。但它究竟是什么?它是数据类型吗?

假设我有一个for循环:

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

我应该用int I吗?或者size_t i;?


当前回答

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

其他回答

Size_t和int是不可互换的。例如,在64位Linux上size_t的大小是64位的(即sizeof(void*)),而int是32位的。

还要注意size_t是无符号的。如果你需要签名版本,那么在某些平台上有ssize_t,它与你的例子更相关。

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

Size_t是一种可以保存任何数组下标的类型。

根据实现的不同,它可以是:

它没有查尔

无符号短

无符号整型

无符号长

未签名的long long

下面是在我的机器的stddef.h中如何定义size_t:

typedef unsigned long size_t;

从维基百科:

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). 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. 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是保证保存任何数组下标的类型。

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

Size_t是无符号类型。因此,它不能表示任何负值(<0)。当你在计算某物,并且确定它不是负的时候,你就会用到它。例如,strlen()返回size_t,因为字符串的长度必须至少为0。

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

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

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

你想求出str2和str1的长度之差。你不能:

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

这是因为赋给diff的值总是一个正数,即使s2 < s1,因为计算是用无符号类型完成的。在这种情况下,根据您的用例,对于s1和s2使用int(或long long)可能会更好。

C/POSIX中有一些函数可以/应该使用size_t,但由于历史原因不使用。例如,fgets的第二个参数理想情况下应该是size_t,但它是int。