词条 | Variable-length array |
释义 |
In computer programming, a variable-length array (VLA), also called variable-sized, runtime-sized, is an array data structure whose length is determined at run time (instead of at compile time).[1] In C, the VLA is said to have a variably modified 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, could be implemented previously with MemoryAllocation
ImplementationC99The following C99 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 In C99, the length parameter must come before the variable-length array parameter in function calls.[1] Linus Torvalds has expressed his displeasure in the past over VLA usage for arrays with predetermined small sizes, with comments like "USING VLA'S IS ACTIVELY STUPID! It generates much more code, and much slower code (and more fragile code), than just using a fixed key size would have done." [6] With the Linux 4.20 kernel, Linux kernel is effectively VLA-free.[7]AdaFollowing is the same example in Ada. Ada arrays carry their bounds with them, so there is no need to pass the length to the Process function. Fortran 90The equivalent Fortran 90 function is 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): CobolThe following COBOL fragment declares a variable-length array of records 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 VLA, unlike that of other languages mentioned here, is safe because COBOL requires one to specify the maximal array size – in this example, C#The following C# fragment declares a variable-length array of integers. The "unsafe" keyword would require an assembly containing this code to be marked as unsafe. References1. ^1 {{cite web |url=http://docs.cray.com/books/004-2179-001/html-004-2179-001/z893434830malz.html |title=Variable Length Arrays |publisher= |archiveurl=https://web.archive.org/web/20180126153326/http://docs.cray.com/books/004-2179-001/html-004-2179-001/z893434830malz.html |archivedate=2018-01-26 |deadurl=yes}} 2. ^{{cite web |url=https://gcc.gnu.org/onlinedocs/gcc/Variable-Length.html |title=Variable Length – Using the GNU Compiler Collection (GCC) |publisher=}} 3. ^ISO 9899:2011 Programming Languages – C 6.7.6.2 4. 4. ^{{cite web |url=https://gcc.gnu.org/onlinedocs/gfortran/Code-Gen-Options.html|title=Code Gen Options - The GNU Fortran Compiler|publisher=}} 5. ^§6.5.3.4 and §7.20.3 of the C11 standard (n1570.pdf) 6. ^{{cite web |title=LKML: Linus Torvalds: Re: VLA removal (was Re: [RFC 2/2] lustre: use VLA_SAFE) |url=https://lkml.org/lkml/2018/3/7/621 |website=lkml.org}} 7. ^{{cite web |title=The Linux Kernel Is Now VLA-Free: A Win For Security, Less Overhead & Better For Clang - Phoronix |url=https://www.phoronix.com/scan.php?page=news_item&px=Linux-Kills-The-VLA |website=www.phoronix.com |language=en}} 1 : Arrays |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。