[英]Can sizeof(size_t) be less than sizeof(int)?
Can sizeof(size_t)
be less than sizeof(int)
? sizeof(size_t)
可以小于sizeof(int)
吗?
Do the C and/or C++ standards guarantee that using unsigned int
for array indexing is always safe? C 和/或 C++ 标准是否保证使用unsigned int
进行数组索引总是安全的?
Yes, sizeof(size_t)
can, in principle, be less than sizeof(int)
.是的, sizeof(size_t)
原则上可以小于sizeof(int)
。 I don't know of any implementations where this is true, and it's likely that there are none.我不知道有任何实现是正确的,而且很可能没有。 I can imagine an implementation with 64-bit int
and 32-bit size_t
.我可以想象一个具有 64 位int
和 32 位size_t
。
But indexing an array with unsigned int
is safe -- as long as the value of the index is within the bounds imposed by the length of the array.但是用unsigned int
索引数组是安全的——只要索引的值在数组长度规定的范围内。 The argument to the []
operator is merely required to be an integer. []
运算符的参数只需要是一个整数。 It's not converted to size_t
.它不会转换为size_t
。 It's defined in terms of pointer arithmetic, in which the +
operator has one argument that's a pointer and another argument that is of any integer type.它是根据指针算术定义的,其中+
运算符有一个参数是指针,另一个参数是任何整数类型。
If unsigned int
is wider than size_t
, then an unsigned int
index value that exceeds SIZE_MAX
will almost certainly cause problems because the array isn't that big.如果unsigned int
比size_t
宽,那么超过SIZE_MAX
的unsigned int
索引值几乎肯定会导致问题,因为数组不是那么大。 In C++14 and later, defining a type bigger than SIZE_MAX
bytes is explicitly prohibited (3.9.2 [compound.types] paragraph 2).在 C++14 及更高版本中,明确禁止定义大于SIZE_MAX
字节的类型(3.9.2 [compound.types] 第 2 段)。 In earlier versions of C++, and in all versions of C, it isn't explicitly prohibited, but it's unlikely that any sane implementation would allow it.在早期版本的 C++ 和所有版本的 C 中,没有明确禁止它,但任何理智的实现都不太可能允许它。
[C answer] [C 答案]
Can
sizeof(size_t)
be less thansizeof(int)
?sizeof(size_t)
可以小于sizeof(int)
吗?
Yes.是的。 The size of size_t
can be less, more or the same as int
as their relative sizes/ranges are not specified in C - only their minimum _MAX
values: 65535, 32767. size_t
的大小可以小于、大于或与int
相同,因为它们的相对大小/范围未在 C 中指定 - 只有它们的最小_MAX
值: _MAX
。
IMO, sizeof(size_t) < sizeof(int)
is a unicorn . IMO, sizeof(size_t) < sizeof(int)
是独角兽。 Theoretical, but not seen.理论上的,但没见过。
Code could use the following to detect such beasties.代码可以使用以下内容来检测此类野兽。
#include <limits.h>
#include <stddef.h>
#if SIZE_MAX < UINT_MAX
#error Unexpected small size_t
#endif
Do the C and/or C++ standards guarantee that using
unsigned int
for array indexing is always safe? C 和/或 C++ 标准是否保证使用unsigned int
进行数组索引总是安全的?
In C, No.在 C 中,没有。
Examples: A small array may only tolerate the indexes of [0 ... 2] - regardless of the type of the index - not the entire range of unsigned
.示例: 一个小数组只能容忍 [0 ... 2] 的索引 - 无论索引的类型如何 - 而不是unsigned
的整个范围。 A huge array may be index-able [0 ... UINT_MAX*42ull
] and so an unsigned
cannot represent all valid indexes.一个巨大的数组可能是可索引的 [0 ... UINT_MAX*42ull
],因此unsigned
不能表示所有有效索引。
A size_t
is wide enough to index all arrays. size_t
足够宽以索引所有数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.