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

1k views Asked by At

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

There are 1 answers

1
Martin Bonner supports Monica On BEST ANSWER

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.