GCC: accuracy of strict aliasing warnings

后端 未结 4 839
囚心锁ツ
囚心锁ツ 2020-12-16 12:19

I\'m trying to check some of my code for strict aliasing violations, but it looks like I\'ve missed something while trying to understand the strict aliasing rule.

Im

4条回答
  •  自闭症患者
    2020-12-16 12:28

    There is nothing inherently wrong with

    sp      = ( unsigned short * )&l;
    

    For all the compiler knows, you might simply cast back the pointer to the correct type again.

    There is also nothing inherently wrong with

    *( sp ) = 1;
    

    It's the combination of the two that's wrong.

    The compiler just can't put the two together to tell you the combination is problematic (it is impossible in the general case, and not so easy in this particular case).

    But

    *( ( unsigned short * )&l ) = 1;
    

    is far easier to detect, and hence the compiler does its best to do so.

提交回复
热议问题