43

There are two ways to allocate memory to an array, of which the size is unknown at the beginning. The most common way is using malloc like this

int * array;
... // when we know the size
array = malloc(size*sizeof(int));

But it's valid too in C99 to define the array after we know the size.

... // when we know the size
int array[size];

Are they absolutely the same?

6
  • 7
    The second, even in C99, is NOT always valid. According to C99 §6.10.8.3 Conditional Feature Macros, an implementation can define _STDC_NO_VLA_ and not implement variable length arrays, and still be in standard compliance. Commented May 21, 2013 at 14:26
  • 10
    This isn't a duplicate question! None of the referenced questions have the details on the differences. Commented May 21, 2013 at 14:33
  • 6
    @WhozCraig AFAIK, that's a 2011 thing. There's no such section in what I have that purports to be a copy of the C99 standard. Commented May 21, 2013 at 14:37
  • 3
    @WhozCraig That's a working draft for what became the 2011 edition of the standard. VLAs are optional in that, they weren't in the 1999 edition. That section was not yet in C99, it was added some time in the 12 years between the standards. (And just in case here is the last draft before the ratification.) Commented May 21, 2013 at 17:42
  • 1
    @DanielFischer thanks for the link, and the clarification, sir. I never go a day without learning something new. Commented May 22, 2013 at 3:04

1 Answer 1

56

No they're not absolutely the same. While both let you store the same number and type of objects, keep in mind that:

  • You can free() a malloced array, but you can't free() a variable length array (although it goes out of scope and ceases to exist once the enclosing block is left). In technical jargon, they have different storage duration: allocated for malloc versus automatic for variable length arrays.
  • Although C has no concept of a stack, many implementation allocate a variable length array from the stack, while malloc allocates from the heap. This is an issue on stack-limited systems, e.g. many embedded operating systems, where the stack size is on the order of kB, while the heap is much larger.
  • It is also easier to test for a failed allocation with malloc than with a variable length array.
  • malloced memory can be changed in size with realloc(), while VLAs can't (more precisely only by executing the block again with a different array dimension--which loses the previous contents).
  • A hosted C89 implementation only supports malloc().
  • A hosted C11 implementation may not support variable length arrays (it then must define __STDC_NO_VLA__ as the integer 1 according to C11 6.10.8.3).
  • Everything else I have missed :-)
Sign up to request clarification or add additional context in comments.

10 Comments

A VLA goes out of scope (ceases to be visible) at the end of the enclosing block because it has block scope. It ceases to exist at the end of the block because it has automatic storage duration. Two different things.
@KeithThompson: To be precise, the identifier for the array goes out of scope at the end of the enclosing blocks. Scopes are properties of identifiers (names), not of objects. An object may be accessible outside the scope of its identifier, as when its address is passed to another routine.
@EricPostpischil: You've out-pedanted me!
@Jens What exactly do you mean by C has no concept of a stack? I have heard this for the first time and am pretty intrigued. Can you please elaborate?
@Daniel main() is the entry, but it may not be the exit. Other functions to terminate a program are exit(), quick_exit(), _Exit() and abort().
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.