Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array size determined at runtime

I'm seeing some code like this:

int foo()
{
  int sz = call_other_func();
  char array[sz];

  /* whatever */
}

I'm baffled at how this would work and even compile with gcc. The size of the array is supposed to be static and determined at compile time, no?

like image 720
lang2 Avatar asked Nov 20 '13 12:11

lang2


People also ask

How do you determine the size of an array in runtime?

So deciding an array size at runtime is possible in modern C (>= C99) and code like the below is fine: int s; printf("Enter the array size: "); scanf("%d", &s); int a[s]; One obvious drawback of VLAs is that if s is quite big and the allocation of a could fail.

Can array size be declared at runtime?

No. In an array declaration, the size must be known at compile time. You can�t specify a size that�s known only at runtime.

Can we declare array size at runtime in C++?

In dynamic arrays, the size is determined during runtime. Dynamic arrays in C++ are declared using the new keyword. We use square brackets to specify the number of items to be stored in the dynamic array. Once done with the array, we can free up the memory using the delete operator.

How is array size calculated?

We can find the size of an array using the sizeof() operator as shown: // Finds size of arr[] and stores in 'size' int size = sizeof(arr)/sizeof(arr[0]);


3 Answers

This type of arrays are called variable length arrays (you would also like to raed: Arrays of Variable Length - GCC) and are allowed in C99 only. By using VLAs, the size of the array can be determine at runtime.

like image 83
haccks Avatar answered Oct 17 '22 20:10

haccks


"In programming, a variable-length array (or VLA) is an array data structure of automatic storage duration whose length is determined at run time (instead of at compile time)." (Wikipedia)

They're supported in C99 (and subsequently in C11).

Read more about how it works: The New C: Why Variable-length Arrays?

like image 23
Eutherpy Avatar answered Oct 17 '22 21:10

Eutherpy


This is valid C99 feature called variable length arrays(VLA), if you compile with gcc -std=c90 -pedantic you will receive the following warning:

warning: ISO C90 forbids variable length array ‘array’ [-Wvla]

using -std=c99 -pedantic will not produce a warning, although both gcc and clang support VLA outside of C99 mode and also in C++ which does not allow VLA as an extension.

We can see from the C99 draft standard section 6.7.5.2 Array declarators paragraph 4 says (emphasis mine):

If the size is not present, the array type is an incomplete type. If the size is * instead of being an expression, the array type is a variable length array type of unspecified size, which can only be used in declarations with function prototype scope;124) such arrays are nonetheless complete types. If the size is an integer constant expression and the element type has a known constant size, the array type is not a variable length array type; otherwise, the array type is a variable length array type.

Note that Visual Studio does not support VLA even though they now support C99

like image 3
Shafik Yaghmour Avatar answered Oct 17 '22 21:10

Shafik Yaghmour