C语言 使用 ssize_t 与 int

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

Using ssize_t vs int

ctype-conversionstandards

提问by hyde

Code

代码

I've got a function which I can write in oneof four possible ways:

我有一个函数,我可以用四种可能的方式之一编写它:

    int do_or_die(int retval);
    int do_or_die(ssize_t retval);
    ssize_t do_or_die(int retval);   
    ssize_t do_or_die(ssize_t retval);   


And then it will be called with bothof these ways for library functions:

然后它将以这两种方式为库函数调用:

    written = do_or_die(write(...)); // POSIX write returns ssize_t
    printed = do_or_die(printf(...)); // printf returns int

Questions

问题

  • Which prototype should I use?
  • What types should I give to writtenand printed?
  • 我应该使用哪个原型?
  • 我应该给什么类型writtenprinted

I want to have the most robust and standard code, while still having just one do_or_diefunction.

我想要最健壮和标准的代码,同时仍然只有一个do_or_die功能。

I am using C99 in this case, but if answer is different for C11, then I'd like to know that too, for future.

在这种情况下,我使用的是 C99,但如果 C11 的答案不同,那么我也想知道,以备将来使用。

回答by Fred Foo

There's no guarantee in the POSIX standard that sizeof(int) >= sizeof(ssize_t), nor the other way around. Typically ssize_tis larger than int, but the safe and portable option in C99 is to use intmax_tinstead for the argument and the return value.

POSIX 标准不能保证 ,反之亦然sizeof(int) >= sizeof(ssize_t)。通常ssize_t大于int,但 C99 中安全且可移植的选项是intmax_t用于参数和返回值。

The only guarantees you have wrt. the relationship between intand ssize_tare:

您拥有的唯一保证。之间的关系intssize_t是:

  • intcan store values of at least the range [-2^15 ... 2^15-1] per ISO C
  • ssize_tcan store values of at least the range [-1 ... 2^15-1] per POSIX (see _POSIX_SSIZE_MAX).
  • int可以存储至少范围 [-2^15 ... 2^15-1] 每个 ISO C 的值
  • ssize_t每个 POSIX 可以存储至少范围 [-1 ... 2^15-1] 的值(请参阅 参考资料_POSIX_SSIZE_MAX)。

(Interestingly, there isn't even a guarantee that ssize_tcan store the negative counterparts of its positive range. It's not a signed size_t, but a "size type" with an error value.)

(有趣的是,甚至不能保证ssize_t可以存储其正范围的负对应项。它不是一个有符号的size_t,而是一个带有错误值的“大小类型”。)

回答by LihO

Use types in a way:

以某种方式使用类型:

  • you don't mix signedand unsignedtypes together and
  • you don't truncate values from larger types while storing them in smaller types (overflow/underflow)
  • 你不混合signedunsigned类型在一起
  • 您不会截断较大类型的值,同时将它们存储在较小的类型中(上溢/下溢)

ssize_tmight be an alias for int, yet it is not standard C and might be environment specific.

ssize_t可能是 的别名int,但它不是标准 C 并且可能是特定于环境的。

If your program will run in specific environment, check whether sizeof(ssize_t) <= sizeof(int)and use int. Otherwise, use some other type Twhere sizeof(T)is greater or equal than both sizeof(int)and sizeof(ssize_t).

如果您的程序将在特定环境中运行,请检查sizeof(ssize_t) <= sizeof(int)并使用int. 否则,使用其他类型T,其中sizeof(T)大于或等于两者sizeof(int)sizeof(ssize_t)