Variable-length array


In computer programming, a variable-length array, also called variable-sized, runtime-sized, is an array data structure whose length is determined at run time.
In C, the VLA is said to have a variably modified type that depends on a value.
The main purpose of VLAs is to simplify programming of numerical algorithms.
Programming languages that support VLAs include Ada, Algol 68, APL, C99 and C#, COBOL, Fortran 90, and J. And also Object Pascal.

Memory

Allocation

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

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. GCC had VLA as an extension before C99.
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, Linux kernel is effectively VLA-free.
Although C11 does not explicitly name a size-limit for VLAs, some readings believe it should have the same maximum size as all other objects, i.e. SIZE_MAX bytes. As a size many orders of magnitude larger than the typical stack guard-page size of 4 KiB and typical total stack size limits on the order of a few megabytes, this reading should not be taken literally.

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 of Float;
function Read_And_Process return Float is
Vals : Vals_Type ;
begin
for I in 1.. N loop
Vals := Read_Val;
end loop;
return Process ;
end Read_And_Process;

Fortran 90

The equivalent Fortran 90 function is

function read_and_process result
integer,intent::n
real::o
real,dimension::vals
integer::i
do i = 1,n
vals = read_val
end do
o = process
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 functions must first be declared, and the array length must be explicitly passed as an argument :

function read_and_process result
integer,intent::n
real::o
real,dimension::vals
real::read_val, process
integer::i
do i = 1,n
vals = read_val
end do
o = process
end function read_and_process

Cobol

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

DATA DIVISION.
WORKING-STORAGE SECTION.
01 DEPT-PEOPLE.
05 PEOPLE-CNT PIC S9 BINARY.
05 DEPT-PERSON OCCURS 0 TO 20 TIMES DEPENDING ON PEOPLE-CNT.
10 PERSON-NAME PIC X.
10 PERSON-WAGE PIC S9V99 PACKED-DECIMAL.

The COBOL VLA, unlike that of other languages mentioned here, is safe because COBOL requires one to specify the maximal array size – in this example, DEPT-PERSON cannot have more than 20 items, regardless of the value of PEOPLE-CNT.

C#

The following C# fragment declares a variable-length array of integers. Prior to 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.

unsafe void DeclareStackBasedArrayUnsafe

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

void DeclareStackBasedArraySafe