第 6.5.6 C99 节,
If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the behavior is undefined
假设size_t
是[0..3]之一。如果 char
数组的大小为 4,索引为 [0..3],最后一个元素后面的一个元素将为索引 4,这会溢出 size_t
。那么,由于这种情况,最大对象大小(以字节为单位)是否为 3,[0..2],最大索引 SIZE_MAX - 1
?换句话说,SIZE_MAX
保证不是有效的数据索引?
第 6.5.3.3 节,
If the promoted type is an unsigned type, the expression ~E is equivalent to the maximum value representable in that type minus E.
始终为2^n-1
,奇数。
最佳答案
首先,一些背景信息:
§7.20.3 ¶2 of the ISO C11 standard规定数据类型 size_t
必须能够表示至少 65535
的数字,即 SIZE_MAX
值必须至少为 65535
。
§5.2.4.1 ¶1规定实现必须支持托管环境中至少 65535
字节的对象大小(但规定独立环境没有这样的要求)。
但是,这两个数字相同并不意味着实现支持的最大对象大小必须等于 SIZE_MAX
。这是因为上面提到的数字仅代表实现必须支持的最低限制。
§6.5.6 ¶8规定在执行指针算术时,如果指针操作数和指针结果都指向同一数组对象的元素,或者超过该数组对象的最后一个元素,则指针不应溢出。
该标准的该部分并未指定数据类型 size_t
必须能够表示对象的所有索引。然而,§6.5.3.4通过声明 sizeof
运算符的结果是表示对象大小(以字节为单位)的 size_t
类型的值,确实暗示了这一点。通过这样做,该标准意味着数据类型 size_t
必须能够表示对象的大小,这也意味着 size_t
必须能够表示索引 1超过对象的最后一个有效索引,因为该索引与对象的大小相同,假设数组元素的类型为 char
。
现在,回答您的问题:
Say
size_t
is one of [0..3]. If achar
array were size 4, index [0..3], one past the last element would be index 4, which overflowssize_t
. Would, then, the maximum object size in bytes be 3, [0..2], maximum indexSIZE_MAX - 1
, because this condition? In other words,SIZE_MAX
is guaranteed to not be a valid index of data?
是的,这是正确的。兼容的实现将无法支持大小为 4
或更大的对象,因为它无法使 sizeof
运算符与它们一起使用,如第 6.5 节中所指定。标准3.4。
关于c - 对象大小限制下的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74839049/