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?

6
  • 1
    What happens if you wish to return the array from the function or store it in a structure? Object lifetime in C is ... fun. Commented Jul 8, 2011 at 19:23
  • Because the storage of a malloc()'d array is different, the heap. Commented Jul 8, 2011 at 19:24
  • you may be interested in this question too: stackoverflow.com/questions/6592318/… Commented Jul 8, 2011 at 20:38
  • 1
    Possible duplicate of malloced array VS. variable-length-array Commented Apr 23, 2016 at 7:37
  • @CiroSantilli六四事件法轮功包卓轩 This question is two years older than the question you linked. Commented Apr 24, 2016 at 22:19

3 Answers 3

12

Two reasons spring to my mind:

  1. Arrays that live beyond this stack frame.
  2. Arrays that are bigger than the stack.
Sign up to request clarification or add additional context in comments.

7 Comments

3. Arrays that you can return from a function. But i guess that is included in number 1.
@hexa that's all there is to item 1
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.
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.
@Roddy I agree with your sentiments 100%, but unfortunately the official term for these things is indeed variable length arrays. Go figure!
|
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.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.