Hello list,
Given the following piece of code
int foo (int i) {
char array[i+1] = {0};
if (array[i]) {
return 0;
}
return 1;
}
When compiling with g++ [g++ (GCC) 6.4.0] the code compiles OK.
But when compiling with clang++ [clang version 4.0.1
(tags/RELEASE_401/final)] I get the following error.
array.c++:2:17: error: variable-sized object may not be initialize
My question now is, is this an bug in GCC, or is there an option for
CLANG to compile this code without errors? Or may is there an option for
g++ to let the compile fail on this code?
Frank
Hello list,
Given the following piece of code
int foo (int i) {
char array[i+1] = {0};
if (array[i]) {
return 0;
}
return 1;
}
When compiling with g++ [g++ (GCC) 6.4.0] the code compiles OK.
But when compiling with clang++ [clang version 4.0.1
(tags/RELEASE_401/final)] I get the following error.
array.c++:2:17: error: variable-sized object may not be initialize
My question now is, is this an bug in GCC, or is there an option for
CLANG to compile this code without errors? Or may is there an option for
g++ to let the compile fail on this code?
C17 6.7.9p3 says:
The type of the entity to be initialized shall be an array of unknown
size or a complete object type that is not a variable length array
type.
Clang is correct to diagnose that code. When I try out various
versions of GCC, they also diagnose your code example
(Compiler Explorer) so I'm not certain what's happening
with your version of GCC.
~Aaron