A variable length array in C (VLA) is an array whose size is decided at run time from a non-constant expression, such as int values[n]; where n is a variable. Introduced in C99, made optional in C11 and still optional in C23, a VLA lives on the stack and disappears when its block ends. This variable length array C guide shows how they work, where they are safe and why many projects avoid them.
Because the size is only known while the program runs, the compiler cannot reserve the space at compile time. It adjusts the stack at the point of declaration instead, which is cheap but has no way to report failure.
A normal array needs a constant size known at compile time. A VLA accepts any integer expression, as long as it is positive when the declaration runs.
With an input of 5 the output is:
Notice that sizeof values is evaluated at run time for a VLA, giving n * sizeof(int). For ordinary arrays sizeof is a compile-time constant. Also notice the check before the declaration: a VLA with a zero, negative or huge size is undefined behaviour, so you must validate the size yourself.
A VLA follows a few fixed rules that differ from normal arrays:
static or extern.int a[n] = {0}; is an error in C; use a loop or memset instead.VLAs shine in function parameters, where the size parameter must come before the array so the compiler knows it. This is the clean way to write functions for two-dimensional data of any shape.
The parameter int m[rows][cols] is really a pointer to an array of cols ints, but the compiler uses cols for the indexing arithmetic. The same function works with a matrix declared with variable dimensions. This usage of variably modified types is the part of the feature that most developers consider genuinely valuable, and it is still supported where VLAs exist.
The stack is small, typically 8 MB on Linux and 1 MB on Windows for the main thread, and often less in worker threads. A VLA takes its space from it, and C gives no way to detect that the allocation failed. If n comes from user input or a file, an attacker or a large value can overflow the stack and crash the program, or worse, corrupt memory.
Compare this with malloc, which returns NULL when it fails, so you can handle the problem. This is why the Linux kernel removed all VLAs from its code, and why many coding standards such as MISRA C forbid them. Compilers can help: -Wvla in GCC and Clang warns on each VLA, and -Wvla-larger-than=1024 in GCC warns when a size may be too big.
| Approach | Size known at | Storage | Failure handling | Lifetime |
|---|---|---|---|---|
Fixed array int a[100] | Compile time | Stack | Not needed | Block |
VLA int a[n] | Run time | Stack | None, undefined behaviour | Block |
malloc(n * sizeof *p) | Run time | Heap | Returns NULL | Until free |
Choose a fixed array when you know a safe upper bound, and malloc when the size is unbounded or large. Use a VLA only for small, validated sizes, or for the typed-parameter feature shown above. The pattern with malloc looks like this:
C99 made VLAs mandatory, C11 made them an optional feature, and C23 keeps that status: a compiler that defines the macro __STDC_NO_VLA__ does not support them. GCC and Clang support them in every C mode. Microsoft's C compiler (MSVC) does not support VLAs, so portable code that targets Windows should avoid them. C++ never adopted VLAs, although some compilers accept them as an extension.
Not in its declaration. Use a loop, memset or memcpy after declaring it. Fixed-size arrays can use = {0}, but a VLA cannot.
No. The size is fixed when the declaration runs. To grow a collection use malloc and realloc.
sizeof on a VLA a compile-time constant?No. It is computed at run time, which is one of the few cases where sizeof evaluates its operand's type at run time.
Rarely. Prefer malloc for variable sizes. Keep VLAs for small, validated sizes or for typed multi-dimensional parameters.
A variable length array is a stack array whose size is set at run time. It cannot be initialised, resized or declared at file scope, and a bad size causes undefined behaviour with no way to detect it. Support is optional since C11, MSVC lacks it, and malloc with a NULL check is the safer default for any size that is not small and checked.