4

C99 allows defining arrays with non-constant size, i.e. the size used to define an array can change on runtime. Code snippet to explain it would be,

void dummy_function1(unsigned int length) {

 char arrA[length];  //Allowed
 .
 .
}

However, it does not allow initializing it in place, i.e.

void dummy_function2(unsigned int length) {

 char arrA[length]={0}; //Not Allowed, compiler throws an error
 char arrB[10]={0};     //Allowed
 .
}

I do not understand, why is this difference in behavior for array which is variable length and the one which is constant length. In both cases, the array would be given memory when the function is invoked.

alk
  • 69,737
  • 10
  • 105
  • 255
  • `length` is not a compile time constant. It will enter by user on run time. At compile time value of `length` is not known to compiler. – haccks Sep 17 '13 at 16:33
  • 1
    This is not a duplicate of the proposed original questions. Those questions ask why a variable-length array cannot be initialized. This question specifically asks why the C standard does not provide for initializing a variable-length array. – Eric Postpischil Sep 17 '13 at 18:24

3 Answers3

4

This is because the compiler does not know how many zeroes to "fill out" the remaining elements with.

The statement

char arrA[3] = { 0 };

can easily be translated to

char arrA[3] = { 0, 0, 0 };

during compile time, while the variable length declaration can not.

Since C does not have a runtime system, the compiler would have to add code to dynamically pad with zeroes depending on the given length. The C standard strives to be minimal with maximum power to the programmer, and therefore things like this are avoided. As of C11, variable-length arrays have been removed from the standard and are labeled as an optional feature.

Johan Henriksson
  • 687
  • 3
  • 10
4

It looks like this is not allowed by the standard if we look at the C99 draft standard section 6.7.8 Initialization paragraph 3 says(emphasis mine):

The type of the entity to be initialized shall be an array of unknown size or an object type that is not a variable length array type.

As to why most likely because supporting initialization of a variable length array would require indeterminate amount of work at run-time.

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • 1
    The question appears to ask why the C 1999 standard does not support initialization of variable-length arrays. This answer does not answer that question. – Eric Postpischil Sep 17 '13 at 18:22
  • @EricPostpischil Hmmm, I could have sworn it did not originally read that way but it very possible I just interpreted differently at the time ... I will update – Shafik Yaghmour Sep 17 '13 at 18:52
2

Hypothesis: C is a medium-level language, and the standards committee did not want to include operations in the basic language (counting the library as separate) that perform amounts of work not determined at compile time.

All the basic operations of C that come to my mind at the moment (perhaps somebody will correct me) require only an amount of work determined at compile time: Calling a function, multiplying two numbers, assigning a structure to another, and so on. These can all be performed with a number of machine instructions executed that is fixed at compile time.

Even allocating space for a variable-length array requires only a compile-time amount of work: Calculate the size and subtract that from the stack pointer. In contrast, initializing all of that space requires an amount of work determined at run time. This is out of character for the C language.

Library routines may require amounts of work determined at run time, such as memset.

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
  • Makes sense, sort of, though VLA still uses an unknown (at compile time) amount of other, often limited resource (stack). After allowing that, fussing about unknown number of CPU cycles and forcing programmer to explicitly call memset (which probably generates identical code after optimizations) seems pointless. – hyde Sep 17 '13 at 18:51
  • +1 for catching the detail the rest of us missed. – Shafik Yaghmour Sep 17 '13 at 18:58