C中的严格别名

发布时间:2020-09-18 发布网站:编程之家
编程之家收集整理的这篇文章主要介绍了C中的严格别名编程之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
关于类型双关语的问题:为什么这段代码会破坏严格的别名规则:

int main()
{
    int a = 1;
    short j;

    printf("%i\n",j = *((short*)&a));
    return 0;
}

这不是:

int main()
{
    int a = 1;
    short j;
    int *p; 

    p=&a;
    printf("%i\n",j = *((short*)p));
    return 0;
}

通过gcc -fstrict-aliasing构建.

谢谢!

解决方法

他们都违反了 strict aliasing rule,我将引用我的 answer here(强调我的前进):

code violates the 07002 which makes it illegal to access an object through a pointer of a different type,although access through a char * is allowed. The compiler is allowed to assume that pointers of different types do not point to the same memory and optimize accordingly.

gcc在-Wstrict-aliasing = n here的文档中更详细一点,其中说:

This option is only active when -fstrict-aliasing is active. It warns about code that might break the strict aliasing rules that the compiler is using for optimization. Higher levels correspond to higher accuracy (fewer false positives). Higher levels also correspond to more effort,similar to the way -O works. -Wstrict-aliasing is equivalent to -Wstrict-aliasing=3.

并描述每个级别如下:

  • Level 1: Most aggressive,quick,least accurate. Possibly useful when
    higher levels do not warn but -fstrict-aliasing still breaks the code,
    as it has very few false negatives. However,it has many false
    positives. Warns for all pointer conversions between possibly
    incompatible types,even if never dereferenced. Runs in the front end
    only.

  • Level 2: Aggressive,not too precise. May still have many false
    positives (not as many as level 1 though),and few false negatives
    (but possibly more than level 1). Unlike level 1,it only warns when
    an address is taken. Warns about incomplete types. Runs in the front
    end only.

  • Level 3 (default for -Wstrict-aliasing): Should have very few false
    positives and few false negatives. Slightly slower than levels 1 or 2
    when optimization is enabled. Takes care of the common pun+dereference
    pattern in the front end: *(int*)&some_float.
    If optimization is
    enabled,it also runs in the back end,where it deals with multiple
    statement cases using flow-sensitive points-to information. Only warns
    when the converted pointer is dereferenced. Does not warn about
    incomplete types.

因此不能保证捕获所有实例,不同级别具有不同程度的准确性.

通常情况下,您正在寻找的效果可以使用通过联合的类型惩罚来完成,我在上面的链接答案和gcc explicitly supports中介绍了这种情况.

总结

以上是编程之家为你收集整理的C中的严格别名全部内容,希望文章能够帮你解决C中的严格别名所遇到的程序开发问题。

如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您喜欢交流学习经验,点击链接加入编程之家官方QQ群:1065694478
编程之家官方公众号

微信公众号搜索 “ 程序精选 ” ,选择关注!

微信公众号搜索 “ 程序精选 ”
精选程序员所需精品干货内容!