VLA in C : Compiler Acceptance Despite Non-Standard Feature
Variable Length Arrays (VLAs), introduced in C99, are not officially supported in C . Yet, compilers such as g and clang surprisingly permit VLA declarations, albeit generating warnings when the -pedantic flag is enabled.
Compiler Acceptance
The compiler's acceptance of VLAs stems from its adherence to a legacy of C compiler behavior. GCC, in particular, maintains compatibility with older C compilers that allowed VLAs.
Standard Implications
Despite the compiler's acceptance, C explicitly forbids VLAs in the array declaration grammar in [dcl.array]: only constant expressions can be used as array size specifiers. In your example, n does not meet this requirement.
Compiler Implementation
Although the standard disallows VLAs, compilers that support them do so through extensions. The specific implementation details vary depending on the compiler. In the case of g and clang , for instance, VLAs are implemented as normal arrays on the stack, with their sizes dynamically allocated during runtime.
The above is the detailed content of Why Do C Compilers Accept VLAs Despite Standard Prohibition?. For more information, please follow other related articles on the PHP Chinese website!