As we already know, VLA (standardized in C99) are not part of the standard in C++, so the code below is "illegal" in C++:
void foo(int n) {
int vla[n];
for (int i = 0; i < n; ++i) {
vla[i] = i;
}
}
Despite of that the compiler (g++ and clang++) accepts the code as valid syntax, producing just a warning in case -pedantic
flag is enabled.
ISO C++ forbids variable length array ‘vla’ [-Wvla]
My questions are:
Why does the compiler accept that declaration?
Can't the compiler just reject an array in which length[is-no-know-at-compile-time]
?
Is there a sort of compatibility syntax rule to follow?What does the standard say about this?
From the assembly code produced, I see the compiler writes in the stack in the loop, like a normal array, but I cannot find anything about the standard behaviour.