C Variable Length Arrays
In this page:
What is a VLA?
A variable length array (VLA) lets you size an array using a variable's value at the moment the array is declared, instead of requiring a compile-time constant like int arr[10]. This is handy when you don't know an array's exact size until the program is already running, such as sizing a buffer to a user-provided count.
Example: What is a VLA?
#include <stdio.h>
int main() {
int n = 5;
int arr[n];
arr[0] = 1;
printf("%d", arr[0]);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Declaring and Using VLAs
To declare a VLA, first compute or read the size into an integer variable, then use that variable directly inside the array brackets, e.g. int n = getCount(); int values[n];. The size expression is evaluated once at the point of declaration, so changing the variable afterward does not resize the array.
Example: Declaring and Using VLAs
#include <stdio.h>
int main() {
int n = 3;
int values[n];
for (int i = 0; i < n; i++) {
values[i] = i * 2;
}
printf("%d", values[2]);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
VLAs inside Functions
VLAs are especially useful inside helper functions that need a temporary buffer sized exactly to the caller's input, avoiding the need to over-allocate a fixed maximum size or call malloc() for something short-lived. The array automatically disappears when the function returns, with no manual cleanup required.
Example: VLAs inside Functions
#include <stdio.h>
void printBuffer(int size) {
int buffer[size];
buffer[0] = 99;
printf("%d", buffer[0]);
}
int main() {
printBuffer(5);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Memory Allocation of VLAs
Unlike malloc(), which reserves memory on the heap that persists until you explicitly free() it, a VLA is allocated on the stack and is automatically reclaimed the moment its enclosing scope ends. This makes VLAs simpler to use for short-lived buffers, since there is no risk of forgetting to free them.
Example: Memory Allocation of VLAs
#include <stdio.h>
void useVLA() {
int n = 4;
int arr[n];
arr[0] = 1;
printf("%d", arr[0]);
}
int main() {
useVLA();
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Limits of VLAs
Stack space is much smaller than heap space (often just a few megabytes), so declaring a VLA sized by an unchecked or attacker-controlled variable can overflow the stack and crash the program. Always validate that a VLA's requested size stays within a sane, expected bound before declaring it.
Example: Limits of VLAs
#include <stdio.h>
int main() {
int n = 10;
int arr[n];
arr[0] = 1;
printf("%d", arr[0]);
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Chapter Quiz — Complete all 20 topics to unlock
0/20 topics done
Complete these topics first:
- C typedef
- C Type Casting
- C Bit Fields
- C Variable Length Arrays
- C Command Line Arguments
- C Function Pointers
- C Callback Functions
- C Multidimensional Pointer
- C string.h Functions
- C stdlib.h Functions
- C math.h Functions
- C time.h Functions
- C ctype.h Functions
- C errno.h
- C assert.h
- C Error Handling
- C Debugging Techniques
- C Code Style & Best Practices
- C Common Mistakes
- C Interview Questions