Questions tagged [variable-length-array]

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.

329 questions
374
votes
12 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
  • 47,252
  • 14
  • 79
  • 112
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
113
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,389
  • 6
  • 21
  • 17
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 […
elslooo
  • 9,814
  • 3
  • 37
  • 57
71
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
  • 15,204
  • 16
  • 73
  • 108
68
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
  • 155,020
  • 33
  • 213
  • 341
66
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
  • 945
  • 7
  • 22
48
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
  • 122
  • 314
  • 481
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
  • 21,860
  • 5
  • 72
  • 158
36
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
  • 691
  • 1
  • 12
  • 24
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
  • 358,771
  • 68
  • 593
  • 989
27
votes
4 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
  • 5,849
  • 5
  • 28
  • 44
24
votes
7 answers

Convert Python sequence to NumPy array, filling missing values

The implicit conversion of a Python sequence of variable-length lists into a NumPy array cause the array to be of type object. v = [[1], [1, 2]] np.array(v) >>> array([[1], [1, 2]], dtype=object) Trying to force another type will cause an…
Marco Ancona
  • 1,709
  • 3
  • 18
  • 31
24
votes
3 answers

Is the operand of `sizeof` evaluated with a VLA?

An argument in the comments section of this answer prompted me to ask this question. In the following code, bar points to a variable length array, so the sizeof is determined at runtime instead of compile time. int foo = 100; double…
PC Luddite
  • 5,551
  • 6
  • 18
  • 38
24
votes
5 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
  • 42,684
  • 17
  • 103
  • 166
1
2 3
21 22