将大双精度转换为无符号整数期间的堆栈损坏
Stack corruption during conversion of large double to unsigned int
在 windows 机器上,运行 visual c++,我发现以下代码行似乎通过调用 __dtoui3 的东西损坏了内存(一堆字节改变在这个调用之后。具体来说,DBL_MAX 的值似乎在内存中的随机位置连续打印两次)
double temp = DBL_MAX;
unsigned int blissfullyUnaware = (unsigned int) temp;
但是,以下情况不适用:
double temp = 0;
unsigned int blissfullyUnaware = (unsigned int) temp;
谁能解释为什么会这样?
[conv.fpint]/1 A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be
represented in the destination type.
强调我的。 DBL_MAX
大约是 2^1024,而 unsigned int
可能达到 2^32。
在 windows 机器上,运行 visual c++,我发现以下代码行似乎通过调用 __dtoui3 的东西损坏了内存(一堆字节改变在这个调用之后。具体来说,DBL_MAX 的值似乎在内存中的随机位置连续打印两次)
double temp = DBL_MAX;
unsigned int blissfullyUnaware = (unsigned int) temp;
但是,以下情况不适用:
double temp = 0;
unsigned int blissfullyUnaware = (unsigned int) temp;
谁能解释为什么会这样?
[conv.fpint]/1 A prvalue of a floating point type can be converted to a prvalue of an integer type. The conversion truncates; that is, the fractional part is discarded. The behavior is undefined if the truncated value cannot be represented in the destination type.
强调我的。 DBL_MAX
大约是 2^1024,而 unsigned int
可能达到 2^32。