Just curious, what actually happens if I define a zero-length array int array[0];
in code? GCC doesn't complain at all.
Sample Program
#include <stdio.h>
int main() {
int arr[0];
return 0;
}
Clarification
I'm actually trying to figure out if zero-length arrays initialised this way, instead of being pointed at like the variable length in Darhazer's comments, are optimised out or not.
This is because I have to release some code out into the wild, so I'm trying to figure out if I have to handle cases where the SIZE
is defined as 0
, which happens in some code with a statically defined int array[SIZE];
I was actually surprised that GCC does not complain, which led to my question. From the answers I've received, I believe the lack of a warning is largely due to supporting old code which has not been updated with the new [] syntax.
Because I was mainly wondering about the error, I am tagging Lundin's answer as correct (Nawaz's was first, but it wasn't as complete) -- the others were pointing out its actual use for tail-padded structures, while relevant, isn't exactly what I was looking for.
In Standard C and C++, zero-size array is not allowed..
If you're using GCC, compile it with
-pedantic
option. It will give warning, saying:In case of C++, it gives similar warning.
Normally, it is not allowed.
However it's been current practice in C to use flexible arrays.
Demonstration:
The idea is that you would then allocate it so:
You might also use it statically (gcc extension):
This is also known as tail-padded structures (this term predates the publication of the C99 Standard) or struct hack (thanks to Joe Wreschnig for pointing it out).
However this syntax was standardized (and the effects guaranteed) only lately in C99. Before a constant size was necessary.
1
was the portable way to go, though it was rather strange0
was better at indicating intent, but not legal as far as the Standard was concerned and supported as an extension by some compilers (including gcc)The tail padding practice, however, relies on the fact that storage is available (careful
malloc
) so is not suited to stack usage in general.It's totally illegal, and always has been, but a lot of compilers neglect to signal the error. I'm not sure why you want to do this. The one use I know of is to trigger a compile time error from a boolean:
If
condition
is a false, then I get a compile time error. Because compilers do allow this, however, I've taken to using:This gives a size of either 1 or -1, and I've never found a compiler which would accept a size of -1.
Zero-size array declarations within structs would be useful if they were allowed, and if the semantics were such that (1) they would force alignment but otherwise not allocate any space, and (2) indexing the array would be considered defined behavior in the case where the resulting pointer would be within the same block of memory as the struct. Such behavior was never permitted by any C standard, but some older compilers allowed it before it became standard for compilers to allow incomplete array declarations with empty brackets.
The struct hack, as commonly implemented using an array of size 1, is dodgy and I don't think there's any requirement that compilers refrain from breaking it. For example, I would expect that if a compiler sees
int a[1]
, it would be within its rights to regarda[i]
asa[0]
. If someone tries to work around the alignment issues of the struct hack via something likea compiler might get clever and assume the array size really is four:
Such an optimization might be reasonable, especially if
myStruct->data
could be loaded into a register in the same operation asmyStruct->size
. I know nothing in the standard that would forbid such optimization, though of course it would break any code which might expect to access stuff beyond the fourth element.An array cannot have zero size.
ISO 9899:2011 6.7.6.2:
The above text is true both for a plain array (paragraph 1). For a VLA (variable length array), the behavior is undefined if the expression's value is less than or equal to zero (paragraph 5). This is normative text in the C standard. A compiler is not allowed to implement it differently.
gcc -std=c99 -pedantic
gives a warning for the non-VLA case.I'll add that there is a whole page of the online documentation of gcc on this argument.
Some quotes:
and
so you could
and boom :-)