C Reference ยท Types and Literals
Variable length arrays in C
Reference for C variable length arrays: runtime-sized local arrays, sizeof evaluated at run time, VLA parameters, stack overflow risks and safer options.
A variable length array (VLA) is an array whose length is decided at run time. You write int values[n]; where n is not a constant expression, and the array is created when execution reaches the declaration. VLAs were added in C99, became optional in C11, and the variably modified types they build on are mandatory again in C23.
VLAs live on the stack, have automatic storage duration only, and cannot be initialized with a list. Because a failed allocation cannot be detected, they are a common source of crashes; most style guides prefer malloc for anything whose size comes from outside the program.
size_t n = 8; int values[n]; /* length known only at run time */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
type name[expr]; /* expr: non-constant integer expression */ void f(size_t n, int a[n]); /* VLA parameter (adjusted to int *) */ void g(size_t r, size_t c, int m[r][c]); typedef int row_t[n]; /* VLA typedef; n evaluated here */
| Part | Description |
|---|---|
expr |
Evaluated once, when the declaration is reached. Must be greater than zero. |
| Parameter sizes | Must be declared earlier in the parameter list; a forward declaration can write int a[*]. |
Returns: sizeof of a VLA is computed at run time and has type size_t.
Errors / undefined behavior: a length of zero or less, or a size that exceeds the available stack, is undefined behavior. Nothing reports the failure; the program simply crashes or corrupts memory. goto or switch jumping past the declaration into its scope is a constraint violation.
Usage notes
- Never size a VLA from user input without a hard cap. Check
nagainst a sensible maximum first, or usemalloc, which returnsNULLon failure. - They cannot be
static,extern, global, or members of a struct or union. File-scope variably modified types are not allowed either. - No initializer list.
int a[n] = {0};is rejected in C99 to C17; C23 allows= {}or= {0}for VLAs. Usememsetfor portable code. - Optional since C11. Check
__STDC_NO_VLA__; if the macro is defined, the compiler does not support them. GCC, Clang and the runner support them. - Pointer-to-VLA types are the clean way to pass 2D data with run-time dimensions:
int (*m)[cols] = malloc(rows * sizeof *m);gives heap storage with the samem[r][c]syntax. - Not C++. C++ has no VLAs, so headers meant to be shared with C++ should avoid them in prototypes.
- Security tools such as
-Wvlaand-Wstack-usageflag them; the Linux kernel removed them entirely.
Related
- Arrays,
sizeof,malloc, Array parameters, Stack and heap - Lessons: Arrays, Dynamic memory