将write(2)始终写入小于或等于SSIZE_MAX吗? [英] Will write(2) always write less than or equal to SSIZE_MAX?

查看:410
本文介绍了将write(2)始终写入小于或等于SSIZE_MAX吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

write(2)的功能签名为ssize_t write(int fd, const void *buf, size_t count).通常,size_t的最大值大于ssize_t的最大值.这是否意味着write实际可以写入的数据量实际上是SSIZE_MAX而不是SIZE_MAX?如果不是这种情况,那么就溢出而言,当写入的字节数大于SSIZE_MAX时会发生什么?

The function signature for write(2) is ssize_t write(int fd, const void *buf, size_t count). Generally, the maximum value of size_t is greater than that of ssize_t. Does this mean the amount of data that write can actually write is actually SSIZE_MAX instead of SIZE_MAX? If that is not the case, what happens when the number of bytes written is greater than SSIZE_MAX with respect to overflows?

我本质上是想知道write写入的数据量是否受SSIZE_MAXSIZE_MAX限制.

I am essentially wondering if that amount of data written by write is bounded by SSIZE_MAX or SIZE_MAX.

推荐答案

类型ssize_t由POSIX定义为一种签名类型,能够存储至少32767个(

The type ssize_t is defined by POSIX as a signed type to be capable of storing at least 32767 (_POSIX_SSIZE_MAX) with no other guarantees. So its maximum value can be less than the maximum value of size_t.

ssize_t 的POSIX定义:

ssize_t's POSIX definition:

ssize_t

用于字节计数或错误指示.

Used for a count of bytes or an error indication.

因此,您请求写入的字节数可能大于ssize_t可以容纳的字节数.在这种情况下,POSIX会将其留给实现.

So it's possible the number of bytes you requested to be written can be greater than what ssize_t can hold. In that case, POSIX leaves it to the implementation.

来自 write() 的POSIX规范:

From write()'s POSIX spec:

ssize_t write(int fildes, const void *buf, size_t nbyte);

如果nbyte的值大于{SSIZE_MAX}, 结果是实现定义的.

If the value of nbyte is greater than {SSIZE_MAX}, the result is implementation-defined.

这篇关于将write(2)始终写入小于或等于SSIZE_MAX吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆