Variable-length array

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.[1] 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, C99 (although subsequently relegated in C11 to a conditional feature, which implementations are not required to support;[2][3] on some platforms, VLAs could be implemented formerly with alloca() or similar functions) and C# (as unsafe-mode stack-allocated arrays), COBOL, Fortran 90, J, and Object Pascal (the language used in Delphi 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 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.[4]

  1. ^ "Variable Length Arrays". Archived from the original on 2018-01-26.
  2. ^ "Variable Length – Using the GNU Compiler Collection (GCC)".
  3. ^ ISO 9899:2011 Programming Languages – C 6.7.6.2 4.
  4. ^ Raymond, Eric S. (2000). "Raymond Software Release Practice Howto: 6. Good development practice". The Linux Documentation Project.