FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Variable-length_array

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;

Fortran 90

edit

The equivalent Fortran 90 function is

floatreadAndProcess(intn){ floatvals[n]; for(inti=0;i<n;++i){ scanf("%f",&vals[i]); } returnprocess(n,vals); }

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; }

C#

edit

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.

DATADIVISION. WORKING-STORAGESECTION. 01 DEPT-PEOPLE. 05 PEOPLE-CNTPIC S9(4)BINARY. 05 DEPT-PERSONOCCURS0 TO20 TIMESDEPENDINGONPEOPLE-CNT. 10 PERSON-NAMEPIC X(20). 10 PERSON-WAGEPIC S9(7)V99PACKED-DECIMAL.

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

edit

Java fixes the size of arrays once they are created, but their size can be determined at runtime.

... SetLength(NumberArray,0); ...

References

edit
Raymond, Eric S. (2000). "Raymond Software Release Practice Howto: 6. Good development practice". The Linux Documentation Project.
  • ↑ "The Linux Kernel Is Now VLA-Free: A Win For Security, Less Overhead & Better For Clang - Phoronix". www.phoronix.com.
  • ↑ §6.5.3.4 and §7.20.3 of the C11 standard (n1570.pdf)
  • ↑

  • View original on Wikipedia | Back | FazBrowse Home