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
  • 827
  • 1
  • 10
  • 17

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
  • 142,963
  • 15
  • 272
  • 331
  • 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
  • 104,019
  • 25
  • 176
  • 264