Home > Backend Development > C++ > Why Do C Compilers Accept VLAs Despite Standard Prohibition?

Why Do C Compilers Accept VLAs Despite Standard Prohibition?

Barbara Streisand
Release: 2024-12-26 04:58:13
Original
291 people have browsed it

Why Do C   Compilers Accept VLAs Despite Standard Prohibition?

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template