gcc, strict-aliasing, and casting through a union

后端 未结 7 557
时光说笑
时光说笑 2020-11-28 06:22

Do you have any horror stories to tell? The GCC Manual recently added a warning regarding -fstrict-aliasing and casting a pointer through a union:

[.

7条回答
  •  难免孤独
    2020-11-28 06:59

    Have you seen this ? What is the strict aliasing rule?

    The link contains a secondary link to this article with gcc examples. http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html

    Trying a union like this would be closer to the problem.

    union a_union {
        int i;
        double *d;
    };
    

    That way you have 2 types, an int and a double* pointing to the same memory. In this case using the double (*(double*)&i) could cause the problem.

提交回复
热议问题