GNU extensions - different behaviour of g++ and clang for taking address of temporary array

Hi all,

Consider a test case:

#include`` <stdio.h>

typedef`` ``int`` a[];

void`` print(``int`` *c) {
printf(``"%d\n"``, c[``0``]);

int`` main() {
```return 0``; }`

g++ -std=gnu++11 -fsyntax-only fails to compile this code: “error: taking address of temporary array”. However, clang accepts it without any warnings and there is no any ASan output for it. Could someone explain me if it a GCC or Clang issue?

It’s a GCC bug – this code is valid. The array to pointer conversion is used to convert the int[1] temporary to a pointer; a reference is not bound to it.

(GCC’s behaviour is not unreasonable, though, and perhaps we should warn on such constructs. I would imagine the GCC diagnostic would be suppressed by making c a pointer to const int?)

Thank you for explanation Richard!
As I see, turning 'c' into 'const int *' doesn't suppress GCC diagnostic. However, this is all information I need.

19.02.2018 21:44, Richard Smith пишет: