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
14
votes
1 answer
Does "int size = 10;" yield a constant expression?
The following code compiles under gcc 4.8 and Clang 3.2:
int main()
{
int size = 10;
int arr[size];
}
8.3.4/1 of the C++ Standard says that the size of an array must be an integral constant expression, which size does not seem to be. Is this a…

KnowItAllWannabe
- 12,972
- 8
- 50
- 91
14
votes
5 answers
Variable length arrays (VLA) in C and C++
I have some concepts about the VLA and its behavior that I need to clarify.
AFIK since C99 it's possible to declare VLA into local scopes:
int main(int argc, char **argv)
{
// function 'main' scope
int size = 100;
int array[size];
…

PaperBirdMaster
- 12,806
- 9
- 48
- 94
14
votes
4 answers
What technical disadvantages do C99-style VLAs have?
I heard from many people that variable length array, introduced in C99, are terrible. Some guys on IRC said a minute ago « I don't think C++ will get VLA's, strousoup made some very negative comments about them ».
What are the reasons why those…

qdii
- 12,505
- 10
- 59
- 116
13
votes
3 answers
Incorrect values when initializing a 2D array to 0 in gcc
#include
using namespace std;
int main() {
int rows = 10;
int cols = 9;
int opt[rows][cols] = {0};
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout <<…

dev_nut
- 2,476
- 4
- 29
- 49
13
votes
4 answers
Sizeof operator with variable-length array type
According to cppreference:
If the type of expression is a variable-length array type, expression
is evaluated and the size of the array it evaluates to is calculated
at run time.
It means: if the type of expression is a VLA type, then…

msc
- 33,420
- 29
- 119
- 214
13
votes
2 answers
Passing a multidimensional array of variable size
I'm trying to understand what "best practice" (or really any practice) is for passing a multidimensional array to a function in c is. Certainly this depends on the application, so lets consider writing a function to print a 2D array of variable…

ABD
- 209
- 1
- 7
12
votes
4 answers
What's the difference between a VLA and dynamic memory allocation via malloc?
I was curious with this:
What is the diference between:
const int MAX_BUF = 1000;
char* Buffer = malloc(MAX_BUF);
and:
char Buffer[MAX_BUF];

Alexis Zecharies
- 139
- 2
- 6
12
votes
2 answers
How is the size of a variable length array computed at runtime in C99?
In C89, the length of an array is known at compile time. But in C99, with variable length arrays, the length of an array may be unknown before runtime.
So how does it get computed?
And why couldn't the length of a dynamically allocated array be…

minh.hieu
- 123
- 5
12
votes
1 answer
Undocumented GCC Extension: VLA in struct
While reading the Clang documentation, I came across the following intriguing tidbit: [1]
clang does not support the gcc extension that allows variable-length arrays in structures. This is for a few reasons: one, it is tricky to implement, two, the…

void-pointer
- 14,247
- 11
- 43
- 61
10
votes
2 answers
Variably-modified types compatibility and its security implications
I'm going through a surge of interest in C99's variably-modified type system. This question was inspired by this one.
Checking the code from this question, I discovered something interesting. Consider this code:
int myFunc(int, int, int,…
user3079266
10
votes
3 answers
Dynamic array allocation on stack in C
I just did a experiment yesterday, and find something confusing:
#include
int main()
{
int j;
scanf("%d",&j);
const int i = j;
int arr[i];
return 0;
}
The number j is read from keyboard and it’s used to allocate the…

user3769509
- 109
- 1
- 1
- 5
9
votes
4 answers
Prototype for variable-length arrays
I am trying to write a function that takes an array of an variable size in c.
void sort(int s, int e, int arr[*]){
...
}
It says that for variable length arrays, it needs to be bounded in the function declaration. What does that mean? I am…

Greg Brown
- 1,227
- 2
- 20
- 44
9
votes
1 answer
C standard regarding sizeof overflowing size_t
Is this undefined behavior? The relevant parts of the standard don't say much.
size_t n = SIZE_MAX / sizeof(double) + 1;
size_t m = sizeof(double[n]);

ov2k
- 295
- 1
- 10
9
votes
1 answer
Variable Length Arrays in C++14?
n3639 proposed the adoption of c99's variable-length-arrays into C++14 (at least for the first dimension.)
But the latest I've been able to find lists n3639 as:
Features in the first CD of C++14, subsequently removed to a Technical…

Jonathan Mee
- 37,899
- 23
- 129
- 288
9
votes
3 answers
Why Are Zero Length VLAs UB?
The 2011 standard explicitly states...
6.7.6.2 Array declarators
If the size is an expression that is not an integer constant expression: if it occurs in a
declaration at function prototype scope, it is treated as if it were replaced by
*;…

Jason
- 3,777
- 14
- 27