9

I'm programming in C99 and use variable length arrays in one portion of my code. I know in C89 zero-length arrays are not allowed, but I'm unsure of C99 and variable length arrays.

In short, is the following well defined behavior?

int main()
{
    int i = 0;
    char array[i];
    return 0;
}
Cornstalks
  • 37,137
  • 18
  • 79
  • 144
  • That should not even compile, seeing as `i` is not compile-time constant. – Jashaszun Jul 09 '13 at 22:34
  • 1
    try compiling that; gcc will come out, punch you, shoot you and steal your car. – gifnoc-gkp Jul 09 '13 at 22:34
  • 5
    [Guys... this is C99... not C89.](http://ideone.com/NKtfJD) – Cornstalks Jul 09 '13 at 22:35
  • 3
    @Jashaszun: In C a local array size is not required to be a constant. – AnT stands with Russia Jul 09 '13 at 22:35
  • 4
    @TheOtherGuy: Variable length arrays are allowed in C99... – Cornstalks Jul 09 '13 at 22:37
  • @Cornstalks you are invoking undefined behavior and even in c99 *if you lie to the compiler, it will get its revenge* – ouah Jul 09 '13 at 22:51
  • @ouah: I'm not trying to lie to the compiler. I just have a variable that ranges from 0 to ~10 that determines how large I need this array to be during each update loop of my program, and I was wondering if variable length arrays had any kind exception to the "no zero length" rule (because if they did, then I wouldn't have to handle the case when my variable is zero separately and explicitly). – Cornstalks Jul 10 '13 at 01:01
  • @TheOtherGuy Proof that you're wrong doesn't matter to you? – Jim Balter Apr 05 '14 at 04:00

3 Answers3

15

No, zero-length arrays are explicitly prohibited by C language, even if they are created as VLA through a run-time size value (as in your code sample).

6.7.5.2 Array declarators

...

5 If the size is an expression that is not an integer constant expression: if it occurs in a declaration at function prototype scope, it is treated as if it were replaced by *; otherwise, each time it is evaluated it shall have a value greater than zero.

Community
  • 1
  • 1
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
2

Zero-length arrays are not allowed in C. Statically typed arrays must have a fixed, non-zero size that is a constant expression, and variable-length-arrays must have a size which evaluates non-zero; C11 6.7.6.2/5:

each time it [the size expression] is evaluated it shall have a value greater than zero

However, C99 and C11 have a notion of a flexible array member of a struct:

struct foo
{
    int a;
    int data[];
};

From C11, 6.7.21/18:

As a special case, the last element of a structure with more than one named member may have an incomplete array type; this is called a flexible array member. In most situations, the flexible array member is ignored. In particular, the size of the structure is as if the flexible array member were omitted except that it may have more trailing padding than the omission would imply. However, when a . (or ->) operator has a left operand that is (a pointer to) a structure with a flexible array member and the right operand names that member, it behaves as if that member were replaced with the longest array (with the same element type) that would not make the structure larger than the object being accessed;

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

Zero-length arrays are not allowed in standard C(not even C99 or C11). But gcc does provide an extension to allow it. See http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html

 struct line {
   int length;
   char contents[0];
 };

 struct line *thisline = (struct line *)
   malloc (sizeof (struct line) + this_length);
 thisline->length = this_length;
Yu Hao
  • 119,891
  • 44
  • 235
  • 294