| FazBrowse Wikipedia Viewer | Main Page | | |
| Viewing: https://en.wikipedia.org/wiki/Variable-length_array |
| Contents |
|---|
| Fortran 90 |
| C# |
| Java |
| References |
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. 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.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;
The equivalent Fortran 90 function is
In C99, the length parameter must come before the variable-length array parameter in function calls. In C11, a int*createIntArray(size_tn){ returnnewint[n]; } intmain(intargc,char*argv[]){ int*a=createIntArray(5); // do something with the array delete[]a; }
The following C# fragment declares a variable-length array of integers. Before 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.
The COBOL VLA, unlike that of other languages mentioned here, is safe because COBOL requires specifying maximum array size. In this example, DEPT-PERSON cannot have more than 20 items, regardless of the value of PEOPLE-CNT.
Java fixes the size of arrays once they are created, but their size can be determined at runtime.
| View original on Wikipedia | Back | FazBrowse Home |