Variable-length array
In computer programming, a variable-length array (VLA), also called variable-sized, runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time).[1] In C, the VLA is said to have a variably modified type that depends on a value (cf. 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, C99 (although subsequently relegated in C11 to a conditional feature which implementations are not required to support;[2][3] on some platforms, could be implemented previously with alloca()
or similar functions) and C# (as unsafe-mode stack-allocated arrays), COBOL, Fortran 90, and J. And also Object Pascal (the language used in Borland Delphi).
Memory
Allocation
- The GNU C Compiler allocates memory for VLAs with automatic storage duration on the stack.[4] VLAs, like all objects in C, are limited to SIZE_MAX bytes.[5]
- VLAs can also be allocated on the heap and accessed using a pointer to VLA.
Implementation
C99
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 read_and_process()
returns.
float read_and_process(int n)
{
float vals[n];
for (int i = 0; i < n; i++)
vals[i] = read_val();
return process(n, vals);
}
In C99, the length parameter must come before the variable-length array parameter in function calls.[1]
Linus Torvalds has expressed his displeasure in the past over VLA usage with comments like "USING VLA'S IS ACTIVELY STUPID! It generates much more code, and much _slower_ code (and more fragile code), than just using a fixed key size would have done." [6]With the in-development Linux 4.20 kernel, Linux kernel is effectively VLA-free.[7]
Ada
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
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.
C#
The following C# fragment declares a variable-length array of integers. The "unsafe" keyword would require an assembly containing this code to be marked as unsafe.
unsafe void declareStackBasedArray(int size)
{
int *pArray = stackalloc int[size];
pArray[0] = 123;
}
References
- ^ a b "Variable Length Arrays".
- ^ "Variable Length - Using the GNU Compiler Collection (GCC)".
- ^ ISO 9899:2011 Programming Languages - C 6.7.6.2 4
- ^ "Code Gen Options - The GNU Fortran Compiler".
- ^ §6.5.3.4 and §7.20.3 of the C11 standard (n1570.pdf)
- ^ "LKML: Linus Torvalds: Re: VLA removal (was Re: [RFC 2/2] lustre: use VLA_SAFE)". lkml.org.
- ^ "The Linux Kernel Is Now VLA-Free: A Win For Security, Less Overhead & Better For Clang - Phoronix". www.phoronix.com.