提示:公众号展示代码会自动折行,建议横屏阅读问题描述前几天进行测试,发现一个神奇的现象:不加任何优化的版本与加了-O2参数的版本测试结果不一致!主要代码类似以下例子:#include
#include
class Foo {
public:
Foo() { printf("%s\n", "ctor"); }
~Foo() { printf("%s\n", "dtor"); }
struct tm {
unsigned int second : 10;
unsigned int minute : 10;
unsigned int hour : 10;
unsigned int unused : 2;
} _tm;
void set(short second) {
// zero out all of the bits.
*(int *)&_tm = 0;
_tm.second = second < 0 ? (second * -1) : second;
if (second < 0) {
// negative the value.
*(int *)&_tm = *(int *)&_tm * -1;
}
}
void output() {
int sign = 1;
if (*(int *)&_tm < 0) {
*(int *)&_tm = *(int *)&_tm * -1;
sign
………………………………