我可以信任的sizeof(为size_t)&LT; = sizeof的(unsigned long int类型)始终是真的吗? [英] Can I trust sizeof(size_t) <= sizeof(unsigned long int) is always true?
问题描述
我可以信任的sizeof(为size_t)&LT; = sizeof的(unsigned long int类型)
始终是真实的,按C89标准
Can I trust sizeof(size_t) <= sizeof(unsigned long int)
is always true, according to C89 standard?
也就是说,我不会,如果我使用洛斯值unsigned long类型
,其中为size_t
预计,反之亦然
i.e., I will not loos value if I use a unsigned long
where size_t
is expected and vice-versa.
推荐答案
也许与为size_t
整数类型发生了什么,并在 C89 是它的基本原理文档。阅读本文件的仔细的第3.4.4节:
Perhaps the best explanation of what happens with size_t
and integer types in C89 is the rationale document of it. Read carefully the section 3.4.4 of this document:
第三段说:
类型的sizeof,不管它是什么,发表的(在库
头),为为size_t,因为它是为程序员有用
能够参考此类型。这一要求隐
限制为size_t是为现有的无符号整数的同义词
型,从而撤销任何概念,最大的申报对象
可能过大跨越,即使一个unsigned long。
The type of sizeof, whatever it is, is published (in the library header ) as size_t, since it is useful for the programmer to be able to refer to this type. This requirement implicitly restricts size_t to be a synonym for an existing unsigned integer type, thus quashing any notion that the largest declarable object might be too big to span even with an unsigned long.
这意味着,对于 C89 ,一般为size_t
是一样的preexisting 无符号
整数型,C89什么意味着 unsigned char型之一,无符号短,无符号整型,无符号长
。结果
特别是,类型的每个值为size_t
是范围无符号长
。结果
通过阅读标准C89的规格,也可以是看到的sizeof(为size_t)&LT; = sizeof的(长)
。
This means that, for the concern of C89, in general size_t
is the same as a preexisting unsigned
integer type, what in C89 implies one of the unsigned char, unsigned short, unsigned int, unsigned long
.
In particular, every value of type size_t
is in the range of unsigned long
.
By reading the specifications of the standard C89, you can see also that sizeof(size_t)<=sizeof(long)
.
现在,在这种情况 C99 有一点不同。该标准指出:
Now, the situation in C99 is a little different. This standard says that:
- 7.17相提并论。 2
为size_t
是一个无符号整数类型。 - 7.17相提并论。 4用于类型
为size_t
[...]不应该有一个整数转换等级大于签订长整型
- ---------除非实现支持大到足以使这个必要的对象。
-
6.2.5相提并论。 8对于任何两个整数类型具有相同的符号性和不同的整数转换等级(见6.3.1.1),以较小的整数转换等级的类型值的范围是其他类型的值的子范围。
- 7.17 par. 2
size_t
is an unsigned integer type. - 7.17 par. 4 The types used for
size_t
[...] should not have an integer conversion rank greater than that ofsigned long int
- --------- unless the implementation supports objects large enough to make this necessary.
6.2.5 par. 8 For any two integer types with the same signedness and different integer conversion rank (see 6.3.1.1), the range of values of the type with smaller integer conversion rank is a subrange of the values of the other type.
由于整数转换的等级
签订长整型是相同的 unsigned long int类型
,这意味着为size_t
的值的范围包含在 unsigned long int类型
的值的范围。
但是,在上面的列表中的项目3敞开了大门,以此规则的例外。
Since the integer conversion rank of signed long int
is the same that unsigned long int
, this implies that the range of values of size_t
is contained in the range of values of unsigned long int
.
But the item 3 in the list above left the door open to exceptions to this rule.
所以,我们只能说,强烈意图的实施,使得为size_t
的价值持有的 unsigned long int类型<范围/ code>。但是,我们不能完全确定。
So, we can only say that is highly intended that an implementation makes the values of size_t
hold in the range of the unsigned long int
. But we cannot be completely sure.
如果您要确定,你可以做以下步骤检查您的系统:
If you want to be sure, you can do the following procedure to check your system:
- 包括文件
&LT; limits.h中&GT;
和&LT; stdint.h&GT;
,以访问关于整数类型的执行情况。 - 比较常数
ULONG_MAX
(从&LT; limits.h中&GT;
)和SIZE_MAX
(从&LT; stdint.h&GT;
)。
- Include the files
<limits.h>
and<stdint.h>
in order to access the information about integer types for your implementation. - Compare the constants
ULONG_MAX
(from<limits.h>
) andSIZE_MAX
(from<stdint.h>
).
一个短节目是这样的:
#include <stdio.h>
#include <limits.h>
#include <stdint.h>
int main(void) {
printf("Is the range of size_t containd in that of unsigned long?\n\n");
if (SIZE_MAX <= ULONG_MAX)
printf("Yes");
else
printf("No");
return 0;
}
这篇关于我可以信任的sizeof(为size_t)&LT; = sizeof的(unsigned long int类型)始终是真的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!