6

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)?
6
  • 1
    To those that may not know, you can turn off this warning with: -Wno-vla. Commented May 15, 2020 at 18:07
  • gcc doesn't complain in C99 / C11 mode. Sounds like a bug. Commented May 15, 2020 at 18:07
  • @FiddlingBits Yes, I know. But thank you very much. Commented May 15, 2020 at 18:08
  • 1
    -Weverything is meant to be an internal test tool for clang; not just a better '-Wall'; llvm advise against using it. (lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20181203/…) Commented May 15, 2020 at 18:39
  • 4
    Conforming compilers can warn about anything they like. See also multi-character constants (like 'ABCD') and trigraphs. Commented May 15, 2020 at 18:42

3 Answers 3

6

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 :-)

Sign up to request clarification or add additional context in comments.

1 Comment

Although all answers are very good and I´m very thankful for each, I choose this answer as most helpful one because it actually provided me some new information with a new point of view and a good reason why this warning shall occur nonetheless.
6

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.

5 Comments

"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". I have a query: is a compiler obliged by the standard to warn about or reject such code?
No, @phlummox. C implementations are required to warn about violations of language constraints (which are explicitly labelled as such in the spec), but otherwise there are no requirements about diagnostics or messaging. Few, if any, well known language extensions represent constraint violations, and VLAs in particular do not. The comment you are responding to is about Clang in particular and its -std=c99 flag, as documented, in particular.
ok. I haven't been able to find good Clang documentation on it, but I the -std flags seem to be closely modelled on gcc's, documented here. If so, then using a -std flag won't necessarily warn about non-standard extensions; only those that directly contradict the standard. To actually disable/warn about non-standard extensions, the -pedantic or -pedantic-errors flag is usually the one you want.
@phlummox, you are confused. Clang's (and GCC's) -std=c99 options cause those compilers to reject (all) code that does not conform to the specified language version. That is their entire point. This is not directly about diagnostics, but diagnostics will indeed be emitted in these cases. It is -pedantic that is a bit uncertain: it (supposedly) causes the compiler to emit all diagnostics required by the language (i.e. diagnose all constraint violations), but the manual notes, because it sometimes surprises people, that not all language extensions involve constraint violations.
Lol. I'm fairly sure I'm not confused. The GCC documentation I linked to explicitly states "When a base standard is specified, the compiler accepts all programs following that standard plus those using GNU extensions that do not contradict it". That is exactly what I stated. It's also true (as you said), that the flags cause "compilers to reject (all) code that does not conform to the specified language version". The GCC developers apparently consider (I express no opinion) that there are programs that are not non-conforming (so needn't be rejected) but which include extensions.
5

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.