ArticleslgStudy

science

Variable-length array

Variable-length array is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Variable-length array rather than just read about it. In short: In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at runtime, instead of at compile time. In the language C, the VLA is said to have a variably modified data type that depends on a value (see Dependent type).

Key takeaways

  • Variable-length array belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Variable-length array to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Variable-length array from memory before moving on to harder problems.

Reference excerpt

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at runtime, instead of at compile time. In the language C, the VLA is said to have a variably modified data type that depends on a value (see Dependent type). The main purpose of VLAs is to simplify programming of numerical algorithms. Programming languages that support VLAs include Ada, ALGOL 68 (for non-flexible rows), APL, C# (as unsafe-mode stack-allocated arrays), COBOL, Fortran 90, J, and Object Pascal (the language used in Delphi and Lazarus, that uses FPC). C99 introduced support for VLAs, although they were subsequently relegated in C11 to a conditional feature, which implementations are not required to support; on some platforms, VLAs could be implemented formerly with alloca() or similar functions. Growable arrays (also called dynamic arrays) are generally more useful than VLAs because dynamic arrays can do everything VLAs can do, and also support growing the array at run-time. For this reason, many programming languages (JavaScript, Java, Python, R, etc.) only support growable arrays. Even in languages that support variable-length arrays, it's often recommended to avoid using (stack-based) variable-length arrays, and instead use (heap-based) dynamic arrays.

Memory

Allocation The GNU Compiler Collection (GCC) for C allocates memory for VLAs with automatic storage duration on the stack. This is the faster and more straightforward option compared to heap-allocation, and is used by most compilers. VLAs can also be allocated on the heap and internally accessed using a pointer to this block.

Implementation

Ada The following is the same example in Ada. Ada arrays carry their bounds with them, so there is no need to pass the length to the Process function.

Fortran 90 The equivalent Fortran 90 function is

when utilizing the Fortran 90 feature of checking procedure interfaces at compile time; on the other hand, if the functions use pre-Fortran 90 call interface, the (external) functions must first be declared, and the array length must be explicitly passed as an argument (as in C):

C Certain C language standards require support for variable-length arrays. Variable-length arrays were never part of the C++ language standard. The following C99 function allocates a variable-length array of a specified size, fills it with floating-point values, and then passes it to another function for processing. Because the array is declared as an automatic variable, its lifetime ends when readAndProcess() returns.

In C99, the length parameter must come before the variable-length array parameter in function calls. In C11, a __STDC_NO_VLA__ macro is defined if VLA is not supported. The C23 standard makes VLA types mandatory again. Only creation of VLA objects with automatic storage duration is optional. GCC had VLA as an extension before C99, one that also extends into its C++ dialect. Linus Torvalds has expressed his displeasure in the past over VLA usage for arrays with predetermined small sizes because it generates lower quality assembly code. With the Linux 4.20 kernel, the Linux kernel is effectively VLA-free. Although C11 does not explicitly name a size-limit for VLAs, some believe it should have the same maximum size as all other objects, i.e. SIZE_MAX bytes. However, this should be understood in the wider context of environment and platform limits, such as the typical stack-guard page size of 4 KiB, which is many orders of magnitude smaller than SIZE_MAX. It is possible to have VLA object with dynamic storage by using a pointer to an array.

C++ While C++ does not support stack-allocated variable length arrays (unlike C which does), they may be allowed by some compiler extensions such as on GCC and Clang. Otherwise, an array is instead heap-allocated, however a collection type such as std::vector is probably better. This is because the existing collection types in C++ automatically use "resource acquisition is initialization" (RAII), and will automatically de-allocate once going out of scope.

C# The following C# fragment declares a variable-length array of integers. Before C# version 7.2, a pointer to the array is required, requiring an "unsafe" context. The "unsafe" keyword requires an assembly containing this code to be marked as unsafe.

C# version 7.2 and later allow the array to be allocated without the "unsafe" keyword, through the use of the System.Span<T> feature.

COBOL The following COBOL fragment declares a variable-length array of records DEPT-PERSON having a length (number of members) specified by the value of PEOPLE-CNT:

The COBOL VLA, unlike that of other languages mentioned here, is safe because COBOL requires specifying maximum array size. In this example, DEPT-PERSON cannot have more than 20 items, regardless of the value of PEOPLE-CNT.

Java Java fixes the size of arrays once they are created, but their size can be determined at runtime.

Object Pascal Object Pascal dynamic arrays are allocated on the heap. In this language, it is called a dynamic array. The declaration of such a variable is similar to the declaration of a static array, but without specifying its size. The size of the array is given at the time of its use.

Removing the contents of a dynamic array is done by assigning it a size of zero.

References

Worked examples

Example 1 — a first encounter with Variable-length array

Start with the simplest possible case. Write down what Variable-length array claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Variable-length array before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Variable-length array ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Variable-length array

In research
Variable-length array appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Variable-length array in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Variable-length array is common in secondary-school and first-year university syllabi. It links to neighbouring topics Arrays, Programming language comparisons, so understanding it makes those chapters shorter.
In everyday life
Look for Variable-length array outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Variable-length array in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Variable-length array means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Variable-length array out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Variable-length array in simple terms?

In computer programming, a variable-length array (VLA), also called variable-sized or runtime-sized, is an array data structure whose length is determined at runtime, instead of at compile time. In the language C, the VLA is said to have a variably modified data type that depends on a value (see De…

Why does Variable-length array matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Variable-length array?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Variable-length array.

Tags

  • Arrays
  • Programming language comparisons

Keep exploring