40

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?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
linusz
  • 743
  • 1
  • 14
  • 26
  • 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.* – WhozCraig May 21 '13 at 14:26
  • 10
    **This isn't a duplicate question**! None of the referenced questions have the details on the differences. – Jens May 21 '13 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. – Daniel Fischer May 21 '13 at 14:37
  • @DanielFischer Page 176, draft 1548, the latest one I have at my disposal, "ISO/IEC 9899:201x" and its in there. Are you saying it was stripped from the final cut? (or is that 201x what you're referring to, which makes more sense now that I think about it). – WhozCraig May 21 '13 at 17:28
  • 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](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) is the last draft before the ratification.) – Daniel Fischer May 21 '13 at 17:42
  • 1
    @DanielFischer thanks for the link, and the clarification, sir. I never go a day without learning something new. – WhozCraig May 22 '13 at 03:04

1 Answers1

53

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 :-)
Jens
  • 69,818
  • 15
  • 125
  • 179
  • 5
    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. – Keith Thompson May 21 '13 at 15:02
  • 1
    @KeithThompson Thanks for the terminology hint; I have edited the answer to be more accurate. – Jens May 21 '13 at 15:09
  • 16
    @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. – Eric Postpischil May 21 '13 at 16:02
  • 7
    @EricPostpischil: You've out-pedanted me! – Keith Thompson May 21 '13 at 17:28
  • 2
    @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? – stillanoob Aug 21 '18 at 08:02
  • 1
    @ibrahim5253 This means the word *stack* does not appear in the ISO C language standard documents. C is stack-agnostic. – Jens Aug 21 '18 at 17:39
  • what if the VLA is initialized In the global scope? Dies that mean it would go out of scope when main() exits? – Daniel Jun 21 '20 at 18:33
  • 1
    @Daniel C does not allow a file-scope VLA. You also can't have `static` VLAs at block scope. – Jens Jun 21 '20 at 20:15
  • @Jens thanks. I'm just learning C myself, and was confused about global scope. That is, since main() is the entry and exit point of the program(?) , a variable declared inside main's body has local scope, since it's inside a function, but its duration would be from the point it's declared until ... the end of the program, ie until main exits, right? ( provided it's not nested under another block) – Daniel Jun 22 '20 at 07:28
  • 2
    @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(). – Jens Jun 22 '20 at 08:52