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语言中的static关键字 在C语言中,static可以用来修饰局...
浅谈C/C++中的指针和数组(二) 前面已经讨论了指针...
浅谈C/C++中的指针和数组(一)指针是C/C++...
从两个例子分析C语言的声明 在读《C专家编程》一书的第三章时...
C语言文件操作解析(一)在讨论C语言文件操作之前,先了解一下...
C语言文件操作解析(三) 在前面已经讨论了文件打开操作,下面...