Variable-length array
≈ 7 min read
Names in other languages
No names in other languages are held for this entry yet.
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
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.
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. type Vals_Type is array (Positive range <>) of Float; function Read_And_Process (N : Integer) return Float is Vals : Vals_Type (1 .. N); begin for I in 1 .. N loop Vals (I) := Read_Val; end loop; return Process (Vals); end Read_And_Process; ===Fortran 90=== The equivalent Fortran 90 function is function read_and_process(n) result(o) integer,intent(in)::n real::o real,dimension(n)::vals integer::i do i = 1,n vals(i) = read_val() end do o = process(vals) end function read_and_process 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): function read_and_process(n) result(o) integer,intent(in)::n real::o real,dimension(n)::vals real::read_val, process integer::i do i = 1,n vals(i) = read_val() end do o = process(vals,n) end function read_and_process float readAndProcess(int n) { float vals[n]; for (int i = 0; i In C99, the length parameter must come before the variable-length array parameter in function calls. In C11, a 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. include include float readAndProcess(int n) { float (*vals)[n] = malloc(sizeof(float[n])); for (int i = 0; i int* createIntArray(size_t n) { return new int[n]; } int main(int argc, char* argv[]) { int* a = createIntArray(5); // do something with the array delete[] a; } unsafe void DeclareStackBasedArrayUnsafe(int size) { int* p = stackalloc int[size]; p[0] = 123; } C# version 7.2 and later allow the array to be allocated without the "unsafe" keyword, through the use of the System.Span feature. using System; void DeclareStackBasedArraySafe(int size) { Span a = stackalloc int[size]; a[0] = 123; } ===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 : DATA DIVISION. WORKING-STORAGE SECTION. 01 DEPT-PEOPLE. 05 PEOPLE-CNT PIC S9(4) BINARY. 05 DEPT-PERSON OCCURS 0 TO 20 TIMES DEPENDING ON PEOPLE-CNT. 10 PERSON-NAME PIC X(20). 10 PERSON-WAGE PIC S9(7)V99 PACKED-DECIMAL. 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 . public class Example { public static int[] createIntArray(int size) { return new int[size]; } public static void main(String[] args) { try { String s = IO.readln("Input an integer for an array size: "); int size = Integer.parseInt(s); int[] a = createArray(size); System.out.printf("int[] of size %d created", size); } catch (NumberFormatException e) { System.err.printf("Invalid integer read: %s%n", e.getMessage()); } } } program CreateDynamicArrayOfNumbers(Size: Integer); var NumberArray: array of LongWord; begin SetLength(NumberArray, Size); NumberArray[0] := 2020; end. Removing the contents of a dynamic array is done by assigning it a size of zero. ... SetLength(NumberArray, 0); ... ==References==
Text derived from https://en.wikipedia.org/w/index.php?oldid=1327211075 under CC BY-SA 4.0; modifications have been made.