0

GCC compiles the following function

void f(int i)
{
    int a[i];
}

I was under the impression that you can only initialize arrays with constant length. Is this supposed to compile, and will it do what I expect it to?

tomKPZ
  • 797
  • 1
  • 7
  • 16

2 Answers2

2

C99 added variable length arrays. And gcc adds this to c89 as an extension with -std=gnu89 option (the default with gcc).

In the latest C Standard, C11, variable length arrays support is marked as optional.

ouah
  • 138,975
  • 15
  • 262
  • 325
  • Does the array get put on the stack or somewhere in the heap using malloc? – tomKPZ Oct 13 '13 at 00:23
  • 1
    @user1887231 variable length arrays can only be declared at block scope and with automatic storage duration, so practically yes they are usually stored in the stack. – ouah Oct 13 '13 at 00:25
0

VLA's are allowed in C99. GCC extention allows it to compile in C89 mode.

haccks
  • 100,941
  • 24
  • 163
  • 252