mardi 21 juillet 2015

Result is not expected when use may_alias to struct

When I use -O2 to compile this code, the result is not what I expected. I expected it works and return 0. However it doesn't change the f1. I wonder if I can use may_alias to struct like this:

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

}

Aucun commentaire:

Enregistrer un commentaire