看啥推荐读物
专栏名称: 腾讯数据库技术
本公众号由腾讯云架构平台部数据库研发中心提供内容支持,旨在分享腾讯海量数据库平台上的技术沉淀,和各位一起探索业界领先的数据库技术!
今天看啥  ›  专栏  ›  腾讯数据库技术

一个编译参数引发的血案

腾讯数据库技术  · 公众号  ·  · 2018-12-20 15:18
提示:公众号展示代码会自动折行,建议横屏阅读问题描述前几天进行测试,发现一个神奇的现象:不加任何优化的版本与加了-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 ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照