Variable-length array
Encyclopedia
In programming, a variable-length array (or VLA) is an array data structure
of automatic storage duration whose length is determined at run time (instead of at compile time).
Programming languages that support VLAs include Ada
, Algol 68
(for non-flexible rows), APL, C
(added in C99
; on some platforms, could be implemented previously with
, Fortran 90
.
, it may not be clear which, if any, of those will store the VLA.
For example, the GNU C Compiler
allocates memory for VLAs on the stack.
function allocates a variable-length array of a specified size, fills it with floating-point values, then passes it to another function for processing. Because the array is declared as an automatic variable, its lifetime ends when the
Following is the same example in Ada
. Note that Ada arrays
carry their bounds with them, there is no need to pass the length to the Process function.
The equivalent Fortran 90
function is:
The following COBOL
fragment declares a variable-length array of records,
The following C# fragment declares a variable-length array of integers. Please note the "unsafe" keyword which would require assembly containing this code to be marked as unsafe.
technically do not provide variable-length arrays, because all array objects in those languages are dynamically allocated on the heap, and therefore do not have automatic storage duration for arrays.
Data structure
In computer science, a data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.Different kinds of data structures are suited to different kinds of applications, and some are highly specialized to specific tasks...
of automatic storage duration whose length is determined at run time (instead of at compile time).
Programming languages that support VLAs include Ada
Ada (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...
, Algol 68
ALGOL 68
ALGOL 68 isan imperative computerprogramming language that was conceived as a successor to theALGOL 60 programming language, designed with the goal of a...
(for non-flexible rows), APL, C
C (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
(added in C99
C99
C99 is a modern dialect of the C programming language. It extends the previous version with new linguistic and library features, and helps implementations make better use of available computer hardware and compiler technology.-History:...
; on some platforms, could be implemented previously with
alloca
or similar functions) and C# (as unsafe-mode stack-allocated arrays), COBOLCOBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....
, Fortran 90
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
.
Memory Allocation
One problem that may be hidden by a language's support for VLAs is that of the underlying memory allocation: in environments where there is a clear distinction between a heap and a stackStack-based memory allocation
Stacks in computing architectures are regions of memory where data is added or removed in a last-in-first-out manner.In most modern computer systems, each thread has a reserved region of memory referred to as its stack. When a function executes, it may add some of its state data to the top of the...
, it may not be clear which, if any, of those will store the VLA.
For example, the GNU C Compiler
GNU Compiler Collection
The GNU Compiler Collection is a compiler system produced by the GNU Project supporting various programming languages. GCC is a key component of the GNU toolchain...
allocates memory for VLAs on the stack.
Examples
The following CC (programming language)
C is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system....
function allocates a variable-length array of a specified size, fills it with floating-point values, then passes it to another function for processing. Because the array is declared as an automatic variable, its lifetime ends when the
read_and_process
function returns.Following is the same example in Ada
Ada (programming language)
Ada is a structured, statically typed, imperative, wide-spectrum, and object-oriented high-level computer programming language, extended from Pascal and other languages...
. Note that Ada arrays
carry their bounds with them, there is no need to pass the length to the Process function.
The equivalent Fortran 90
Fortran
Fortran is a general-purpose, procedural, imperative programming language that is especially suited to numeric computation and scientific computing...
function is:
The following COBOL
COBOL
COBOL is one of the oldest programming languages. Its name is an acronym for COmmon Business-Oriented Language, defining its primary domain in business, finance, and administrative systems for companies and governments....
fragment declares a variable-length array of records,
DEPT-PERSON
, having a length (number of members) specified by the value of PEOPLE-CNT
.The following C# fragment declares a variable-length array of integers. Please note the "unsafe" keyword which would require assembly containing this code to be marked as unsafe.
Dynamic vs. automatic
Languages such as JavaJava (programming language)
Java is a programming language originally developed by James Gosling at Sun Microsystems and released in 1995 as a core component of Sun Microsystems' Java platform. The language derives much of its syntax from C and C++ but has a simpler object model and fewer low-level facilities...
technically do not provide variable-length arrays, because all array objects in those languages are dynamically allocated on the heap, and therefore do not have automatic storage duration for arrays.