Rohit Saxena
Rohit Saxena

Reputation: 61

shouldn't dynamically allocating the size of a C array give an error ?

int n=10;
int arr[n];

this code works fine in my GCC compiler. Isn't the size of static array is allocated at compilation time ? Shouldn't this code generate an error ?

Upvotes: 0

Views: 179

Answers (1)

Shafik Yaghmour
Shafik Yaghmour

Reputation: 158459

Variable length arrays are a C99 feature(optional in C11) and gcc supports this as an extension when not in c99 mode, one quick way to see this with gcc is to use the following:

gcc -std=c89 -pedantic

You will see the following warning:

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

but if you build using gcc -std=c99 -pedantic you will not see any warnings. From the C99 draft standard section 6.7.5.2 Array declarators paragraph 4:

[...] 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.

Upvotes: 2

Related Questions