假设sizeof(double)> = sizeof(void *)是否安全? [英] Is it safe to assume sizeof(double) >= sizeof(void*)?
问题描述
可以安全地假设sizeof(double)
将始终大于或等于sizeof(void*)
吗?
Is it safe to assume that sizeof(double)
will always be greater than or equal to sizeof(void*)
?
在某种情况下,以下内容可移植吗?
To put this in some context, is the following portable?
int x = 100;
double tmp;
union {
double dbl;
void* ptr;
} conv;
conv.ptr = (void*)&x;
tmp = conv.dbl;
conv.dbl = tmp;
printf("%d\n", *((int*)conv.ptr));
它确实可以在我测试过的几台机器上运行,但是我发现这很可怕如果sizeof(void*) > sizeof(double)
是错误的.
It does work on the few machines that I've tested it on, but I can see this going horribly wrong if sizeof(void*) > sizeof(double)
.
推荐答案
在当前系统上可以. double
在所有当前和将来的系统上都是64位,因为它们与IEEE算术的双精度一致.指针将来不太可能但肯定会更大-可能不是为了更大的地址空间,而是为了携带边界信息.
On current systems yes. double
is 64 bits on all current and future systems because they're aligned with IEEE arithmetic's double-precision. It's unlikely but certainly possible that pointers could be larger in the future - probably not for the sake of larger address space, but instead for carrying with them bounding information.
在任何情况下,依靠double
和void *
之间的任何关系似乎是一个非常糟糕的主意...
In any case it seems like a really bad idea to rely on any relationship between double
and void *
...
这篇关于假设sizeof(double)> = sizeof(void *)是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!