I just did a experiment yesterday, and find something confusing:
#include <stdio.h>
int main()
{
int j;
scanf("%d",&j);
const int i = j;
int arr[i];
return 0;
}
The number j
is read from keyboard and it’s used to allocate the array arr
on the stack.
The compiler does not even know the size of the array at compile time (initializes j to 0?), but there is no compilation error. How is it possible?
Variable length arrays were added to C99. It's described in the C99 rationale:
6.7.5.2 Array declarators
C99 adds a new array type called a variable length array type. The
inability to declare arrays whose size is known only at execution time
was often cited as a primary deterrent to using C as a numerical
computing language. Adoption of some standard notion of execution time
arrays was considered crucial for C’s acceptance in the numerical
computing world.
The number of elements specified in the declaration of a variable
length array type is a runtime expression. Before C99, this size
expression was required to be an integer constant expression.
There is no "dynamic array allocation on the stack". The array size has to be specified at the declaration.
Some compilers, like GCC allow them as an extension in C90 (in GCC, this is equivalent to ansi and C89) mode and C++. In these cases, you will get a warning (-Wpedantic
) or an error (-Werror
or -pedantic-errors
). Consult the documentation for your compiler.
Per @Deduplicator's comment, you seem to have a misconception. Variable length arrays cannot be declared static.
§ 6.7.6.2
10 EXAMPLE 4
All declarations of variably modified (VM)
types have to be at either block scope or function prototype scope.
Array objects declared with the _Thread_local
, static
, or extern
storage-class specifier cannot have a variable length array (VLA)
type. However, an object declared with the static
storage-class
specifier can have a VM type (that is, a pointer to a VLA type).
Finally, all identifiers declared with a VM type have to be ordinary
identifiers and cannot, therefore, be members ostructures or unions.
This means that static
storage and automatic
storage are mutually exclusive.
For some delving into how allocating a variable amount of memory on the stack can work, see this delving into how a compiler can implement the (non-standardized) alloca()
function:
Alloca implementation
The C99 standard offers Variable Length Arrays ("VLA") with essentially the same functionality; although the memory is reclaimed on a per-scope basis rather than a per-function basis:
What's the difference between alloca(n) and char x[n]?
There are some reasons to be hesitant to use either too aggressively with unbounded size. There's no way to check if stack memory is available as you can test for whether heap memory is available via. NULL from malloc()
. If your variable length array is too large it will cause a stack overflow and undefined behavior; true for both methods of stack allocation:
Why is the use of alloca() not considered good practice?
C has such a feature as Variable Length Arrays. Arrays with automatic storage duration can be defined in the flight.