4

This is wrong since a variable sized object may not be initialized

int size = 4;
int array[size] = {1};

size is a variable, but doesn't the compiler know its value when it creates array (Isn't size assigned an initial value of 4 at compile-time?)? Let size change after that, why would it be an issue? I mean, these are consecutive instructions, what could possibly alter the value of size before the array is declared?

Second question: Why is this not allowed:

const int size = 4;
int array[size] = {1};

I am declaring size as a const. I know that const != read-only, and that declaring size as a macro is the correct way to go about it. But if I promise the compiler using const that I wont change the value of size, why is it not allowed?

Korizon
  • 3,677
  • 7
  • 37
  • 52

1 Answers1

6

The answer to the first question is "because the language specification says so". Although a compiler may be able to infer the size of the array, doing so requires some static analysis which is not trivial when the array size is not a compile-time constant expression.

As to why initialization of VLAs is not allowed: one reason I can think of is that it is not known until runtime how many elements they will contain, so a VLA may be shorter than its initializer list, which would invoke undefined behavior. I can't tell for sure if this is (one of) the real reason(s), though.

Second question: Why is this not allowed:

It is allowed, unless you the writers of your compiler are living in a cave (for example, the engineers over at Microsoft do -- their C compiler is one rare example of a widely used compiler that still does not support C99, 15 years after its standardization.). Any modern, decent C compiler should enable you to use variable-length arrays which are present in C99. Compilers that already implement C11 may or may not opt to support VLAs (as it's an optional feature of the latest standard).

  • gcc-4.7.3 still complains about `const int size=4; int array[size] = {1};` – Korizon Jan 15 '14 at 18:34
  • @KVM And? It's perfectly fine. As you observed correctly, you can't initialize a VLA. –  Jan 15 '14 at 18:35
  • What i mean by `why is it not allowed` was why does declaring `size` as a const _not_ allow me to initialize the array. I am curious to know the rationale behind it. If I am promising the compiler I wont alter the value of `size`, initializing the VLA should be allowed – Korizon Jan 15 '14 at 18:42
  • @KVM Because a `const` is not a constexpr. Again, see #1, I answered *just that.* –  Jan 15 '14 at 18:42
  • `doing so requires some static analysis which is not trivial when the array size is not a compile-time constant expression` Evaluation of a const is compile time, right? – Korizon Jan 15 '14 at 18:44
  • @KVM It (usually) is. Associating the value with the name is less trivial, and there may be cases when even the value of a `const` may change (for example, `volatile` qualified `const`s in an embedded system -- it's really a "variable", changeable by the hardware, but code is only allowed to read it.) –  Jan 15 '14 at 18:46
  • 2
    @KVM: a `const`-qualified variable is not a *constant expression* (compile-time constant) in C; it's simply a runtime variable whose value may not be modified after initialization. A *constant expression* would be a numeric literal, a macro that expands to a numeric literal, an arithmetic expression consisting only of numeric literals or macros, or a macro that expands to such an expression, such as `#define ARR_SIZE WORDS + SPACES + 1`. – John Bode Jan 15 '14 at 19:14