应该使用size_t或ssize_t [duplicate]

时间:2022-05-04 08:55:14

This question already has an answer here:

这个问题已经有了答案:

At my code, I do not use int or unsigned int. I only use size_t or ssize_t for portable. For example:

在我的代码中,我不使用int或unsigned int.我只使用size_t或ssize_t作为可移植。例如:

typedef size_t intc;    // (instead of unsigned int)
typedef ssize_t uintc;  // (instead of int)

Because strlen, string, vector... all use size_t, so I usually use size_t. And I only use ssize_t when it may be negative.

因为strlen、字符串向量……所有的都使用size_t,所以我通常使用size_t。我只用ssize_t,当它可能是负的时候。

But I find that:

但是我发现:

The unsigned integer types are ideal for uses that treat storage as a bit array. Using an unsigned instead of an int to gain one more bit to represent positive integers is almost never a good idea. Attempts to ensure that some values are positive by declaring variables unsigned will typically be defeated by the implicit conversion rules.

无符号整数类型是将存储处理为位数组的理想类型。使用无符号而不是int来获得更多的位来表示正整数几乎不是一个好主意。通过声明未签名的变量来确保某些值是正数的尝试通常会被隐式转换规则击败。

in the book The C++ Programming Language.

在《c++编程语言》一书中。

So I am puzzled. Am I wrong? Why does the STL not abide by the suggest on the book?

所以我困惑。我错了吗?为什么STL不遵守书上的建议?

2 个解决方案

#1


93  

ssize_t is used for functions whose return value could either be a valid size, or a negative value to indicate an error. It is guaranteed to be able to store values at least in the range [-1, SSIZE_MAX] (SSIZE_MAX is system-dependent).

ssize_t用于返回值可以是有效大小的函数,或者是表示错误的负值的函数。它保证能够至少在[-1,SSIZE_MAX]范围内存储值(SSIZE_MAX与系统相关)。

So you should use size_t whenever you mean to return a size in bytes, and ssize_t whenever you would return either a size in bytes or a (negative) error value.

因此,当您打算以字节为单位返回大小时,您应该使用size_t,并在每次返回大小为字节或(负)错误值时使用ssize_t。

See: http://pubs.opengroup.org/onlinepubs/007908775/xsh/systypes.h.html

参见:http://pubs.opengroup.org/onlinepubs/007908775/xsh/systypes.h.html

#2


19  

ssize_t is not included in the standard and isn't portable. size_t should be used when handling the size of objects (there's ptrdiff_t too, for pointer differences).

ssize_t不包括在标准中,也不是可移植的。当处理对象的大小时,应该使用size_t(对于指针差异,也有ptrdiff_t)。

#1


93  

ssize_t is used for functions whose return value could either be a valid size, or a negative value to indicate an error. It is guaranteed to be able to store values at least in the range [-1, SSIZE_MAX] (SSIZE_MAX is system-dependent).

ssize_t用于返回值可以是有效大小的函数,或者是表示错误的负值的函数。它保证能够至少在[-1,SSIZE_MAX]范围内存储值(SSIZE_MAX与系统相关)。

So you should use size_t whenever you mean to return a size in bytes, and ssize_t whenever you would return either a size in bytes or a (negative) error value.

因此,当您打算以字节为单位返回大小时,您应该使用size_t,并在每次返回大小为字节或(负)错误值时使用ssize_t。

See: http://pubs.opengroup.org/onlinepubs/007908775/xsh/systypes.h.html

参见:http://pubs.opengroup.org/onlinepubs/007908775/xsh/systypes.h.html

#2


19  

ssize_t is not included in the standard and isn't portable. size_t should be used when handling the size of objects (there's ptrdiff_t too, for pointer differences).

ssize_t不包括在标准中,也不是可移植的。当处理对象的大小时,应该使用size_t(对于指针差异,也有ptrdiff_t)。