When I compile this experiment code:
int main(void)
{
int foo = 5;
char bar[foo];
}
with clang and the '-Weverything'
or respectively the separate '-Wvla'
flag combined with the '-std=c99'
flag,
I still get the warning:
warning: variable length array used [-Wvla]
although C99 conform implementations shall, in comparison to later C standards (C11, C18, etc.) - where the VLA-support is optional, support variable length arrays without exception.
'-std=c99'
flag in clang?There are various reasons why one might want to avoid using variable length arrays in a program, even when they are guaranteed to be supported in the version of the language you are using. One, as John Bollinger mentioned, is in case you want to maintain compatibility with other standards that don't support them.
Another is that, on typical stack-based implementations, they consume an amount of stack that may not be known at compile time, or that an untrusted user may be able to affect, and such implementations typically don't have a good way to detect or recover from stack overflow. For instance, the Linux kernel developers have decided for this reason that VLAs should not be used in the kernel, and using -Wvla
helps them enforce this.
These issues will certainly not apply in every program, and that is why the -Wvla
option is an option; you can turn it on if, for whatever reason, you want to know about uses of VLAs in your program, and you can leave it off if you don't. You chose to use -Weverything
which turns on all warnings that exist. This flag is not really intended to be useful for programmers, since as you've noticed, it turns on many warnings that are only meant to be useful in certain situations to people that know they want them. It's meant instead for helping to debug the compiler itself.
So in effect, you have told the compiler "issue all the warnings you can, even if they're not relevant in my situation", and now you're asking why you got a warning that wasn't relevant in your situation :-)