当使用may_alias结构时,不期望结果

时间:2015-07-21 14:09:51

标签: c++11 gcc struct

当我使用-O2编译此代码时,结果不符合我的预期。我预计它会起作用并返回0.但它并没有改变f1。我想知道我是否可以使用may_alias来构造这样的结构:

struct my_struct1
{

    int i[2];
    char a;
};


typedef struct my_struct1 __attribute__((__may_alias__))  t_my_struct1;

int main (void)
{

    float f1=4.0e+4;
    t_my_struct1 *sptr2 = (t_my_struct1 *)&f1;
    sptr2->i[0] = 0;
    sptr2->i[1] = 0;
    if(f1 != 0.0f)
        return 1;// may_alias doesn't work

    return 0;// may_alias works

}

0 个答案:

没有答案