问题
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.
- Why do I still get this warning for using a VLA with the
'-std=c99'
flag in clang? - Is this a bug or is that just for the hint to take care for the compliance to implementations conforming to later C standards (as well as C89/C90)?
回答1:
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 :-)
回答2:
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.
回答3:
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:
- VLAs may pose portability issues to non fully conforming implementations, and there are quite a few of those. Use
-Wno-vla
to disable this warning. - some combinations of operators may be caused by a programmer error or confusion about relative precedence that is sometimes counter-intuitive. The expression is still conform to the C Standard, yet the compiler will issue a warning and the programmer can add parentheses to prevent the warning.
- assignments in a test expression are conforming expressions, yet the compiler will issue a warning as they might be simple typos, where
==
was misspelt as=
. - unused variables are not errors, but will produce warnings under
-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.
来源:https://stackoverflow.com/questions/61825661/why-does-clang-complain-about-using-variable-length-arrays-with-std-c99-flag