Does casting a char array to another type violate strict-aliasing rules?

╄→尐↘猪︶ㄣ 提交于 2020-01-01 08:27:08

问题


Consider these two functions:

int f1()
{
  alignas(int) char buf[sizeof(int)] = {};
  return *reinterpret_cast<int*>(buf);
}

int f2()
{
  alignas(int) char buf[sizeof(int)] = {};
  char* ptr = buf;
  return *reinterpret_cast<int*>(ptr);
}

GCC warns that the first violates strict-aliasing rules. But the second is OK.

Clang accepts both without complaint.

Is the warning legitimate?


回答1:


The warning is legitimate. f2 is not OK (it is undefined behaviour), it just doesn't provoke the warning.

I suspect the reason that f2 doesn't provoke the warning is that:

int f3()
{
    int i = 0;
    char *ptr = reinterpret_cast<char*>(&i);
    return *reinterpret_cast<int*>(ptr);
}

Is entirely legal. You can use a char* (or a void*) as a "universal pointer" - provided you cast back to the right type before access. GCC is clearly being careful to avoid warning about f3, at a cost of not warning about f2.

Clang is failing to warn about either f1 or f2 - but it is not required to.



来源:https://stackoverflow.com/questions/41298619/does-casting-a-char-array-to-another-type-violate-strict-aliasing-rules

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!