8

According to c99 standard, we can write the following code and it's totally legal

int x;
scanf("%d",&x);
int ar[x];

My question is, if I can allocate an array like this, why would I ever need malloc to allocate variable size arrays again?

Also, could you please explain how does the variable length arrays allocation happens? Deep inside, does it call malloc to allocate the array or what?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Fingolfin
  • 5,363
  • 6
  • 45
  • 66

3 Answers3

12

Two reasons spring to my mind:

  1. Arrays that live beyond this stack frame.
  2. Arrays that are bigger than the stack.
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • 3. Arrays that you can return from a function. But i guess that is included in number 1. – Vinicius Kamakura Jul 08 '11 at 19:28
  • 3
    @hexa that's all there is to item 1 – David Heffernan Jul 08 '11 at 19:31
  • thanks for the answers, guys, it makes perfect sense now. I still have something though, how is the variable length array implemented inside C? assuming I am using gcc compiler. – Fingolfin Jul 08 '11 at 19:35
  • Oh, sorry. I used a bad phrase, I actually meant that. Still, how is it implemented by the compiler? is there a simple way that you guys can explain it? Again, thanks a lot. – Fingolfin Jul 08 '11 at 19:49
  • I understand that it is up to the implementator to decide how to allocate variable length arrays (VLAs). I actually think variable length array is a terrible nomenclature since they never vary in size once allocated. All the same, that's what they are called. GCC allocates them on the stack with a simple stack pointer increment. But other implementations are free to put them on the heap if they so desire. – David Heffernan Jul 08 '11 at 19:58
  • @Adel - It's allocated off the stack like a 'normal' array. It's not strictly a variable length array, as the length is fixed and can't be changed unlike c++ vectors. (which are basically `malloc`d) – Roddy Jul 08 '11 at 20:00
  • 1
    @Roddy I agree with your sentiments 100%, but unfortunately the official term for these things is indeed variable length arrays. Go figure! – David Heffernan Jul 08 '11 at 20:01
5

Variable length array allocation (or any array declaration actually) is done on the stack (assuming GCC compiler). Malloc assigns memory from the heap.

Two advantages to heap vs. stack: 1. Stack is much smaller. There is a decent chance that your variable-size array could cause your stack to overflow. 2. Items allocated on the stack don't survive after the function they were declared in returns.

sshannin
  • 2,767
  • 1
  • 22
  • 25
  • 1
    Your 'advantage' #2 is also a potential disadvantage and frequent cause of memory leaks. Arrays on the stack (used with care) are often good practice. – Roddy Jul 08 '11 at 20:03
2

In the C11 standard, variable length arrays became "optional" which I take to mean "implementation defined" and as such they are no longer portable.

This is shown in

6.7.6.2 Array declarators section 4

Variable length arrays are a conditional feature that implementations need not support.

and

6.10.8.3 Conditional feature macros section 1

__STDC_NO_VLA__ The integer constant 1, intended to indicate that the implementation does not support variable length arrays or variably modified types.

Some advantages of using malloc over the VLA are:

  • The implementation of malloc usually obtains memory from the heap which in most C implememtations is a bigger resource than the stack. However neither heap nor stack are mentioned in the C standard, but they are common ways to implement global and local memory.
  • Memory obtained from malloc can be increased or reduced by realloc but it is not possible with a VLA.
  • Memory obtained from malloc can be passed around the program until freed, with pointers, but the VLA can only be used in a hierarchy of functions. The VLA becomes dead after the function in which it is defined returns, because it goes out of scope.
Weather Vane
  • 33,872
  • 7
  • 36
  • 56