A variable length array is an array in C99 and other languages whose size is unknown at compile time; instead, it's determined at runtime.
Questions tagged [variable-length-array]
413 questions
449
votes
10 answers
Why aren't variable-length arrays part of the C++ standard?
I haven't used C very much in the last few years. When I read this question today I came across some C syntax which I wasn't familiar with.
Apparently in C99 the following syntax is valid:
void foo(int n) {
int values[n]; //Declare a variable…

Andreas Brinck
- 51,293
- 14
- 84
- 114
148
votes
10 answers
C compile error: "Variable-sized object may not be initialized"
Why do I receive the error "Variable-sized object may not be initialized" with the following code?
int boardAux[length][length] = {{0}};

helloWorld
- 2,929
- 7
- 24
- 19
135
votes
5 answers
Can't understand this way to calculate the square of a number
I have found a function that calculates square of a number:
int p(int n) {
int a[n]; //works on C99 and above
return (&a)[n] - a;
}
It returns value of n2. Question is, how does it do that? After a little testing, I found that between…

Emanuel
- 1,333
- 1
- 10
- 11
100
votes
2 answers
Correctly allocating multi-dimensional arrays
The intent of this question is to provide a reference about how to correctly allocate multi-dimensional arrays dynamically in C. This is a topic often misunderstood and poorly explained even in some C programming books. Therefore even seasoned C…

Lundin
- 195,001
- 40
- 254
- 396
76
votes
9 answers
Difference between array type and array allocated with malloc
Today I was helping a friend of mine with some C code, and I've found some strange behavior that I couldn't explain him why it was happening. We had TSV file with a list of integers, with an int each line. The first line was the number of lines the…

Jorge Leitao
- 19,085
- 19
- 85
- 121
71
votes
2 answers
What does "[*]" (star modifier) mean in C?
While trying to implement a C11 parser (for educational purposes), I found that in C11 (p. 470) but also in C99 (p. 412) (thanks Johannes!), the direct declarator is defined as:
(6.7.6) direct-declarator:
direct-declarator […

cutsoy
- 10,127
- 4
- 40
- 57
67
votes
5 answers
How does the compiler allocate memory without knowing the size at compile time?
I wrote a C program that accepts integer input from the user, that is used as the size of an integer array, and using that value it declares an array of given size, and I am confirming it by checking the size of the array.
Code:
#include…

Rahul
- 975
- 9
- 25
50
votes
4 answers
Is there any overhead for using variable-length arrays?
Is there some overhead of using variable-length arrays? Could the size of array be passed via command line argument at run time? Why is it introduced, compared to automatic and dynamically allocating an array?

Tim
- 1
- 141
- 372
- 590
45
votes
4 answers
How does alloca() work on a memory level?
I'm trying to figure out how alloca() actually works on a memory level. From the linux man page:
The alloca() function allocates size bytes of space in the stack
frame of the caller. This temporary space is automatically freed
when the function…

glades
- 3,778
- 1
- 12
- 34
40
votes
6 answers
What's the point of VLA anyway?
I understand what variable length arrays are and how they are implemented. This question is about why they exist.
We know that VLAs are only allowed within function blocks (or prototypes) and that they basically cannot be anywhere but on the stack…

Shahbaz
- 46,337
- 19
- 116
- 182
40
votes
1 answer
malloced array VS. variable-length-array
There are two ways to allocate memory to an array, of which the size is unknown at the beginning. The most common way is using malloc like this
int * array;
... // when we know the size
array = malloc(size*sizeof(int));
But it's valid too in C99 to…

linusz
- 743
- 1
- 14
- 26
36
votes
3 answers
C++ replacement for C99 VLAs (goal: preserve performance)
I am porting some C99 code that makes heavy use of variable length arrays (VLA) to C++.
I replaced the VLAs (stack allocation) with an array class that allocates memory on the heap. The performance hit was huge, a slowdown of a factor of 3.2 (see…

Szabolcs
- 24,728
- 9
- 85
- 174
34
votes
4 answers
Does C++ support Variable Length Arrays?
No, wait, bear with me...
VLAs were always a GCC extension, but they were adopted by C99:
[C99: 6.7.5.2/4]: If the size is not present, the array type is an
incomplete type. If the size is * instead of being an expression, the
array type is a…

Lightness Races in Orbit
- 378,754
- 76
- 643
- 1,055
33
votes
5 answers
Enabling VLAs (variable length arrays) in MS Visual C++?
How can I enable the use of VLAs, variable length arrays as defined in C99, in MS Visual C++ or that is not possible at all?
Yes I know that the C++ standard is based on C89 and that VLAs are not available in C89 standard and thus aren't available…

Shinnok
- 6,279
- 6
- 31
- 44
33
votes
2 answers
How does GCC implement variable-length arrays?
How does GCC implement Variable-length arrays (VLAs)? Are such arrays essentially pointers to the dynamically allocated storage such as returned by alloca?
The other alternative I could think of, is that such an array is allocated as last variable…

box
- 3,156
- 3
- 26
- 36