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
7
votes
2 answers
Why doesn't this code generate an error on using a variable array size?
The code below should generate an error, since there is no way that the compiler can know the array size during compilation.
int f;
std::cin >> f;
int c[f];
c[100] = 5;
I am compiling with gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2 and it doesn't just…

v010dya
- 5,296
- 7
- 28
- 48
7
votes
2 answers
Complete encapsulation without malloc
I was experimenting with C11 and VLAs, trying to declare a struct variable on the stack with only an incomplete declaration. The objective is to provide a mechanism to create a variable of some struct type without showing the internals (like the…

Mabus
- 1,418
- 12
- 20
7
votes
6 answers
ISO C90 forbids variable length array
I'm dynamically calculating the size of an array. Something like:
void foo(size_t limit)
{
char buffer[limit * 14 + 1];
}
But just GCC compiler says:
error: ISO C90 forbids variable length array ‘buffer’
searching on SO I found this answer:
C99…

Jack
- 16,276
- 55
- 159
- 284
6
votes
4 answers
Matrices as function parameters in C89
For most of my undergrad C programming course, we studied C99 and our lecturer never bothered to teach us the main differences between C99 and previous versions.
We have recently been informed that there's a possibility we'll be asked to implement…

Samuele B.
- 481
- 1
- 6
- 29
6
votes
2 answers
Why do I need dynamic memory allocation if I can just create an array?
I was reading about dynamic memory allocation and static memory allocation and found the following about dynamic memory allocation:
In the programs seen in previous chapters, all memory needs were determined before program execution by defining the…

Vasi Marin
- 453
- 1
- 3
- 9
6
votes
4 answers
variable length field in Database
Is there any way to store variable length list into database in SQL or any other database engine ??
Here list means no. of elements in the array. For e.g. an array of numbers.
But in my actual application things to be stored are objects and hence…

Mahesh Gupta
- 2,688
- 9
- 30
- 46
6
votes
3 answers
Array size determined at runtime
I'm seeing some code like this:
int foo()
{
int sz = call_other_func();
char array[sz];
/* whatever */
}
I'm baffled at how this would work and even compile with gcc. The size of the array is supposed to be static and determined at compile…

lang2
- 11,433
- 18
- 83
- 133
5
votes
1 answer
Declaring variable-sized arrays in assembly
I'm writing an assembly program which I want to be able to do the (basic) following:
x = 100;
y = int[x]
E.g. the size of y depends on the value of x.
NOTE: I am using NASM instruction set on a 64 bit Ubuntu system.
In assembly I know that the size…

Pete Hamilton
- 7,730
- 6
- 33
- 58
5
votes
2 answers
C initialized and non initialized array with variable size
I have the next two code examples:
const char *val = strchr(ch, ' ');
const int diff = (int)(val - ch);
char arr[diff];
and
const char *val = strchr(ch, ' ');
const int diff = (int)(val - ch);
char arr[diff] = {0};
The second one generates the…

user1570891
- 87
- 9
5
votes
2 answers
When and how are VLAs evaluated in sizeof expressions?
The C Standard has this language:
6.5.3.4 The sizeof and _Alignof operators
Semantics
The sizeof operator yields the size (in bytes) of its operand, which may be an expression or the parenthesized name of a type. The size is determined from the…

chqrlie
- 131,814
- 10
- 121
- 189
5
votes
3 answers
why can C++ "fill" initialize a variable-sized array?
#include
using namespace std;
void aa(int n) {
int test[n] = {0};
}
int main() {
aa(10);
return 0;
}
and got
error: variable-sized object may not be initialized
but
#include
using namespace std;
void aa(int n) {
…

jin zhenhui
- 105
- 4
5
votes
3 answers
How to use Visual Studio as an IDE with variable length array(VLA) working?
In any normal compiler for C or C++, variable length arrays are working normally but in Visual Studio Community 2019, VLAs are not working. How can i in any way use Visual Studio as an IDE (becasue i like it's features) and still have VLAs in C and…

Pera
- 173
- 2
- 14
5
votes
2 answers
Referencing a yet-to-be-mentioned function parameter using the new-style function declarations
Compelled to use the variable length array feature for my auxiliary function that prints square matrices, I defined it as follows:
void print_matrix(M, dim)
unsigned dim;
int M[dim][dim];
{
/* Print the matrix here. */
...
The…

undercat
- 529
- 5
- 17
5
votes
2 answers
If a compiler defines __STDC_NO_VLA__, does it still have to support flexible array members?
In C99, flexible array members (of a structure) and variable length arrays were mandatory parts of the standard — conforming C99 compilers (implementations) have to support them both.
In C11, an implementation is allowed to define (§6.10.8.3…

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278
5
votes
4 answers
Can i make a dynamic array this way?
So my code is this :
int a, b;
printf("Give dimensions\n");
scanf("%d %d", &a, &b);
double array1[a][b];
printf("Give values\n");
int i, j;
for(i=0; i

vailanter
- 51
- 3