Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 755
John Zwinck Avatar asked Dec 23 '16 09:12

John Zwinck


1 Answers

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.

like image 161
Martin Bonner supports Monica Avatar answered Nov 10 '22 09:11

Martin Bonner supports Monica