HOME

TheInfoList



OR:

In
computer programming Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, a variable-length array (VLA), also called variable-sized or runtime-sized, is an
array data structure In computer science, an array is a data structure consisting of a collection of ''elements'' (values or variables), each identified by at least one ''array index'' or ''key''. An array is stored such that the position of each element can be co ...
whose length is determined at run time (instead of at compile time). In C, the VLA is said to have a variably modified type that depends on a value (see
Dependent type In computer science and logic, a dependent type is a type whose definition depends on a value. It is an overlapping feature of type theory and type systems. In intuitionistic type theory, dependent types are used to encode logic's quantifiers lik ...
). The main purpose of VLAs is to simplify programming of numerical algorithms. Programming languages that support VLAs include
Ada Ada may refer to: Places Africa * Ada Foah, a town in Ghana * Ada (Ghana parliament constituency) * Ada, Osun, a town in Nigeria Asia * Ada, Urmia, a village in West Azerbaijan Province, Iran * Ada, Karaman, a village in Karaman Province, ...
,
Algol 68 ALGOL 68 (short for ''Algorithmic Language 1968'') is an imperative programming language that was conceived as a successor to the ALGOL 60 programming language, designed with the goal of a much wider scope of application and more rigorously d ...
(for non-flexible rows), APL,
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
(although subsequently relegated in
C11 C11, C.XI, C-11 or C.11 may refer to: Transport * C-11 Fleetster, a 1920s American light transport aircraft for use of the United States Assistant Secretary of War * Fokker C.XI, a 1935 Dutch reconnaissance seaplane * LET C-11, a license-build var ...
to a conditional feature, which implementations are not required to support; on some platforms, VLAs could be implemented previously with
alloca Stack (abstract data type)#Hardware_stack, Stacks in computing architectures are regions of memory (computers), memory where data is added or removed in a LIFO (computing), last-in-first-out (LIFO) manner. In most modern computer systems, each ...
()
or similar functions) and C# (as unsafe-mode stack-allocated arrays),
COBOL COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily us ...
, Fortran 90, J, and
Object Pascal Object Pascal is an extension to the programming language Pascal (programming language), Pascal that provides object-oriented programming (OOP) features such as Class (computer programming), classes and Method (computer programming), methods. ...
(the language used in
Borland Delphi Delphi is a general-purpose programming language and a software product that uses the Delphi dialect of the Object Pascal programming language and provides an integrated development environment (IDE) for rapid application development of desktop, ...
and Lazarus, that uses FPC). 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 programming languages that do 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 C Compiler The GNU Compiler Collection (GCC) is an optimizing compiler produced by the GNU Project supporting various programming languages, hardware architectures and operating systems. The Free Software Foundation (FSF) distributes GCC as free software ...
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


C99

The following
C99 C99 (previously known as C9X) is an informal name for ISO/IEC 9899:1999, a past version of the C programming language standard. It extends the previous version ( C90) with new features for the language and the standard library, and helps impl ...
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) 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, one that also extends into its C++ dialect.
Linus Torvalds Linus Benedict Torvalds ( , ; born 28 December 1969) is a Finnish software engineer who is the creator and, historically, the lead developer of the Linux kernel, used by Linux distributions and other operating systems such as Android. He also ...
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 The Linux kernel is a free and open-source, monolithic, modular, multitasking, Unix-like operating system kernel. It was originally authored in 1991 by Linus Torvalds for his i386-based PC, and it was soon adopted as the kernel for the GNU ope ...
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-like syntax with dynamic storage by using a pointer to an array. float read_and_process(int n)


Ada

The following is the same example in
Ada Ada may refer to: Places Africa * Ada Foah, a town in Ghana * Ada (Ghana parliament constituency) * Ada, Osun, a town in Nigeria Asia * Ada, Urmia, a village in West Azerbaijan Province, Iran * Ada, Karaman, a village in Karaman Province, ...
. 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 COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily us ...
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 COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily us ...
VLA, unlike that of other languages mentioned here, is safe because
COBOL COBOL (; an acronym for "common business-oriented language") is a compiled English-like computer programming language designed for business use. It is an imperative, procedural and, since 2002, object-oriented language. COBOL is primarily us ...
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(int size) 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(int size)


Object Pascal

Object Pascal Object Pascal is an extension to the programming language Pascal (programming language), Pascal that provides object-oriented programming (OOP) features such as Class (computer programming), classes and Method (computer programming), methods. ...
dynamic arrays are allocated on the heap. Michaël Van Canneyt
"Free Pascal Reference guide: Dynamic arrays"
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. program CreateDynamicArrayOfNumbers(Size: Integer); var NumberArray: array of LongWord; begin SetLength(NumberArray, Size); NumberArray := 2020; end. Removing the contents of a dynamic array is done by assigning it a size of zero. ... SetLength(NumberArray, 0); ...


References

{{Reflist Arrays