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]
Example here
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?However, Clang supports such variable length arrays for compatibility with GNU C and C99 programs. If you would prefer not to use this extension, you can disable it with -Werror=vla.
The biggest problem is that one can not even check for failure as they could with the slightly more verbose malloc'd memory. Assumptions in the size of an array could be broken two years after writing perfectly legal C using VLAs, leading to possibly very difficult to find issues in the code.
In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see Dependent type).
If you want a "variable length array" (better called a "dynamically sized array" in C++, since proper variable length arrays aren't allowed), you either have to dynamically allocate memory yourself: int n = 10; double* a = new double[n]; // Don't forget to delete [] a; when you're done!
You are correct that conforming C99 implementations must support VLAs, whereas implementations can conform to later C standards without supporting VLAs. But I think you're missing the forest for the trees: I take the aforementioned difference between the standards to be exactly the point of the warning. It's not saying that your code, as you are now building it, may break. Rather, it is warning that you are relying on a feature that is not universally forward compatible, so that if you try to build your code somewhere else then it may break.
If Clang thought that VLAs were a non-standard extension to C99, then it ought not to accept your VLA-using code at all in -std=c99
mode.
C99 conforming implementations must support VLAs, but enabling all warnings with -Weverything
means please compiler, tell me about anything that may pose a problem. Among potential problems are many conforming constructs:
-Wno-vla
to disable this warning.==
was misspelt as =
.-Weverything
The list goes on. If you ask for more warnings, which a life saver in most circumstances, you might want to refine your CFLAGS
to disable some selected warnings such as -Wno-vla
to compile your programs if you indeed use VLAs on purpose.
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 :-)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With