A Variable-Length Array (VLA) in C is an array data structure whose length is determined at runtime rather than at compile time. Introduced in the C99 standard, VLAs allow the dimension of an array to be specified using a non-constant integer expression. The term “variable” refers exclusively to the evaluation of the size at runtime; once a VLA is instantiated, its size remains fixed for its entire lifetime.Documentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
Syntax and Declaration
A VLA is declared similarly to a standard array, but the size specifier is a variable or an expression evaluated during execution.Technical Constraints and Rules
Working with VLAs introduces specific compiler rules and memory behaviors that differ from standard fixed-size arrays: 1. Struct and Union Membership VLAs cannot be members of astruct or union. This is a strict language constraint. Developers frequently confuse VLAs with Flexible Array Members (FAMs), but FAMs are structurally different (they must be the last member of a struct and have an incomplete type [], relying on heap allocation).
malloc, VLA allocation failure cannot be detected. Because VLAs are typically allocated on the stack frame, evaluating a size expression that exceeds available stack memory results in an uncatchable stack overflow and undefined behavior. There is no mechanism to return a NULL pointer if the allocation fails.
3. Control Flow and Scope Jumping
It is illegal to jump into the scope of a VLA from outside of its scope using goto or switch statements. Doing so bypasses the runtime evaluation and allocation of the array, leaving the VLA in an unallocated or invalid state.
static or extern storage class specifiers.
sizeof Operator and Side Effects
For standard arrays, sizeof is evaluated at compile time, and its operand is never executed. For VLAs, the sizeof operator is evaluated at runtime. The runtime environment calculates the total bytes based on the evaluated length expression multiplied by the size of the data type.
Crucially, because the operand of sizeof is evaluated at runtime when applied to a VLA, any side effects within the sizeof operand will execute.
Function Prototypes and VLA Parameters
When passing multidimensional VLAs to functions, the variable dimensions must be declared in the parameter list before they are used in the array declarator.* to denote a VLA type without specifying the exact variable name.
Standardization Note
While introduced as a mandatory feature in C99, VLAs were made an optional feature in the C11 standard to accommodate implementations where stack memory is highly constrained (e.g., embedded systems). Compilers indicate the lack of VLA support by defining the__STDC_NO_VLA__ macro.
Master C with Deep Grasping Methodology!Learn More





