Variable length array

from Wikipedia, the free encyclopedia

A variable length array (VLA) is a data type in the C programming language . It concerns a field ( English array ), the size of which only at runtime, i. H. variable, is set.

Functionality and application

In contrast to the classic vector , the size of which is determined at the time of translation, the C99 standard introduces the concept of variable array sizes. When defining a field, its size can be determined by any integer expression.

Example vector and VLA
void foo(unsigned int len) {        
    int vector[3] = { 14, 4, 7 };  /* initialisiertes Feld fixer Größe */
    int vla[len];                  /* VLA entsprechend dem Parameter len */
    ...
}

It should be noted that VLAs cannot be initialized (restriction in the C standards, cf. §6.7.8 or §6.7.9) and the size can only be specified once during the definition. In addition, VLAs can only be created within functions. Outside of functions, field sizes must also be known in C99 at the time of translation. As with all local variables, the scope of a VLA begins with its definition and includes the enclosing block. A great advantage of VLAs is that there is no need to call malloc()and for temporary fields free(); the compiler takes over the request and release of the required memory. Since VLAs are usually created on the stack and not on the heap , the memory requirement is also considerably more efficient. In addition, fixed array sizes, which are often the cause of software errors, can be dispensed with.

example

The following example shows a typical application of VLAs.

Example reverse the character sequence of a string of any length
#include <stdio.h>
#include <string.h>

char* reverse_string(char* in_out){
	unsigned int len = strlen(in_out); 
	char tmp[len + 1]; // VLA für String und Nullterminierung
	strcpy(tmp, in_out);
	for (int i = 0; i < len; i++)
		in_out[i] = tmp[len - i - 1];	
	return in_out;
}

int main(void) {
	char tmp[] = "Hallo";
	printf("%s\n", reverse_string(tmp));
	return 0;
}

The function reverse_string()uses the local variable tmpas VLA; this takes up the passed string. The string is then written back to the input / output parameter in reverse order in_out. Of course, multi-dimensional VLAs can also be generated, as the example from the C11 standard showsint a[n][6][m];

Effects

sizeofWith VLAs, the operator must be evaluated at runtime, i. H. meta information must be generated for each VLA at runtime. The definition of VLAs must not be skipped, here the C standards restrict the use of goto. In contrast to C99, the implementation of VLAs is optional for C11-compliant compilers. The availability can be __STDC_NO_VLA__queried via the define , which has the integer value 1, if VLAs are not supported.

Differentiation from other programming languages

VLAs are C-specific and are not adopted by the C ++ standard. C ++ offers container classes that enable variable fields. The programming languages APL and Perl support fields, the size of which can be changed at runtime. Arrays in Java are of a fixed size. As they are created dynamically, as with VLAs, the sizes can be set at runtime.

Individual evidence

  1. Kernighan / Ritchie: Programming in C - With the C reference manual in German , Hanser 1990, 2nd edition
  2. a b ISO / IEC9899-1999 Programming Languages ​​- C
  3. a b c d ISO / IEC9899-2011 Programming Languages ​​- C
  4. Randy Meyers: The New C: Why Variable Length Arrays? , Dr. Dobb's, October 1, 2001, Online: http://www.drdobbs.com/the-new-cwhy-variable-length-arrays/184401444 . Retrieved August 12, 2014
  5. a b c Derek M. Jones: The New C Standard - An Economic and Cultural Commentary , Addison-Wesley Professional 2008, also online: http://www.coding-guidelines.com/cbook/cbook1_2.pdf . Retrieved August 12, 2014
  6. Website of the CERT Secure Coding Standard STR35-C . Retrieved February 14, 2016