3

Give an a basic set of C code:

int a = 5;
int b[a] = {1,2,3,4,5};

Why does this generate this error?

$gcc -o demo demo.c
error: variable-sized object may not be initialized

I understand that it is a bad thing to do and the compiler won't let you. However as a teacher I am having a hard time coming up with an explanation as to why you can not and should not do this.

(Assume C99)

How would you explain what is happening here?

David Milanese
  • 370
  • 4
  • 17
  • Because variables are variables - they can change anytime in the program. – abhishek_naik May 18 '16 at 09:13
  • 3
    @CinCout C supports VLA. But doesn't support the initialization of such arrays using braces initalizer list. – Mohit Jain May 18 '16 at 09:15
  • @MohitJain, yes. But why? – David Milanese May 18 '16 at 09:17
  • You don't know the count of element, how would you prepare initializer list. If you know at compile time, you don't need VLA feature. Rule is documented in section 6.7.8/3. – Mohit Jain May 18 '16 at 09:19
  • @MohitJain, can you provide a link please. – David Milanese May 18 '16 at 09:22
  • 1
    Are you asking for a [design rationale](http://www.open-std.org/jtc1/sc22/wg14/www/C99RationaleV5.10.pdf) for the C language? – Kerrek SB May 18 '16 at 09:22
  • @KerrekSB. A little. It comes down to understanding at what point does the compiler initialize the array, b? Is this done during preprocessing or runtime? – David Milanese May 18 '16 at 09:30
  • I still don't understand the heart of your question. Are you asking *how C works*, or *why C works the way it does*? The first one is easy (`a` is not a constant expression). – Kerrek SB May 18 '16 at 09:40
  • "historical reasons" – M.M May 18 '16 at 09:48
  • @DavidMilanese obviously this initialization could not be carried out until the size is known, e.g. what if the size at runtime is 3, or 7. we would have to discard some initializers, or insert some zeroes ... – M.M May 18 '16 at 09:50
  • @KerrekSB, Both are valid questions, but I am more interested in how C looks at that code and in what order processes it. The next step would be to look at why (if it's not obvious). – David Milanese May 18 '16 at 14:23
  • @M.M, Yes, however, as a compiler why should that matter. You can initialise an array of length 3 with only e.g. 2 elements, or 15 elements. The compiler will give you a warning but it won't give you an error. – David Milanese May 18 '16 at 14:24
  • @DavidMilanese too many initializers is an error . – M.M May 18 '16 at 20:29
  • @M.M, on gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-4), `demo1e.c:5:2: warning: excess elements in array initializer [enabled by default] int b[3] = { 1, 5, 9, 4 }; ^ demo1e.c:5:2: warning: (near initialization for ‘b’) [enabled by default] ` – David Milanese May 19 '16 at 00:05
  • @DavidMilanese gcc often says "warning" for errors – M.M May 19 '16 at 02:09

1 Answers1

1

Why does this generate this error?

C standard doesn't allow this. Variable length arrays can't be initialized using brace-enclosed list of initializers.

Standard says:
C11-§6.7.9/3:

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

haccks
  • 104,019
  • 25
  • 176
  • 264