0

我有以下结构

struct {
    int myData;
    int myAnotherData;
}Value;

struct AnotherStructure {
    unsigned int uiLowData;
    unsigned int uiHighData;
};

AnotherStructure m_AnotherStructure;
Value val;
val.myData = 10;
#define MULTIPLY 36000000000

unsigned __int64 &internalStructure = *(unsigned __int64*)&m_AnotherStructure;
internalStructure  = 0;
internalStructure += ((unsigned __int64)val.myData * MULTIPLY );

我的问题是在上述情况下是否存在任何数据溢出,因为我们将 unsigned int 与大值相乘,结果是否存储在 unsigned int 类型的临时值中,然后存储在 int 64 中?如果现在怎么不会有任何溢出?

谢谢

4

1 回答 1

0

val.myData在乘法之前被强制转换为 unsigned __int64 ,因为您显式地强制转换。仍然可能发生溢出,具体取决于 val.myData 中存储的值 - 最大 int 乘以 36000000000 不适合 64 位。你用演员表松开你的代数符号。

你应该试试这个:

struct AnotherStructure {
    int64_t uiLowData;
    int64_t uiHighData;
};
// signed_128bit_integer: look into your compiler documentation
signed_128bit_integer &internalStructure = *(signed_128bit_integer*)&m_AnotherStructure;
internalStructure  = 0;
internalStructure += ((signed_128bit_integer)val.myData * MULTIPLY );
于 2012-07-12T08:03:27.313 回答