r/C_Programming • u/Araneidae • Jul 11 '26
Question Surprising bug with zero sized array initialiser
Consider the following code:
#include <stdio.h>
struct s { int a; int *b; };
struct s s1 = { .a = 1, .b = (int[2]) {}};
struct s s2 = { .a = 2, .b = (int[0]) {}};
int main(void)
{
printf("s1.a = %d, s2.a = %d\n", s1.a, s2.a);
return 0;
}Now let's compile and run this with gcc and clang:
$ gcc -o test test.c && ./test
s1.a = 1, s2.a = 0
$ clang -o test test.c && ./test
s1.a = 1, s2.a = 2These are both recent versions of the respective compilers (on Fedora):
$ gcc --version |head -n1
gcc (GCC) 15.2.1 20260123 (Red Hat 15.2.1-7)
$ clang --version |head -n1
clang version 21.1.8 (Fedora 21.1.8-4.fc43)Interesting result. It seems that with gcc the use of a zero sized static array initialiser triggers a bug which silently erases the entire enclosing structure! This seems to be quite an old bug (I can reproduce this on a variety of gcc versions), and I have not managed to find any warnings that catch this.
If I use the -pedantic flag on clang it tells me that I'm using c23 extensions, but even so invoking gcc -std=c23 (or gnu23) makes no difference to this bug.
Back in the day there were functional mailing lists where one could report oddities like this, but today I find I have no idea where to report this! Vaguely hoping that this subreddit is relevant.
9
u/SmokeMuch7356 Jul 11 '26 edited Jul 11 '26
I think you may have found a hole in the language definition. You cannot declare an array object with zero size:
N3220:
A declaration like
is a constraint violation and a diagnostic is required. However I'm not seeing any language about 0-size array casts or compound literal types.
To me this smells like a case of undefined behavior; i.e., not a bug as such (at least, not a bug in the compiler).
24
u/aocregacc Jul 11 '26
you can report it on the bugtracker, but it looks like it's a known issue:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=65673
13
u/sciencekm Jul 11 '26
Microsoft's compiler refuses to compile this with:
error C2466: cannot allocate an array of constant size 0
4
u/grigus_ Jul 11 '26
Clearly it's an issue. In which scenario, in real life, one is using that initializer for .b member of the structure?
Please explain me, because I don't really understand. Thx
1
u/alex_sakuta Jul 11 '26
Interesting bug. How did you come to find it?
I would never find such a bug since I never use extensions.
3
u/ReallyEvilRob Jul 11 '26
I don't think it's a bug. This seems more like UB.
2
u/babysealpoutine Jul 11 '26
Interesting. I wonder if it's just initialising a, not the "whole struct," because b is zero-sized; it would be interesting to see what it does if s2 has a, b, and c members, with c being the zero-sized array.
Yep, I think that its just overwriting the member before the zero-sized array. https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html
1
u/EndlessProjectMaker Jul 11 '26
FWIW
On macos, gcc-15 from brew has the bug too.
[N.B. the default gcc is just an alias of clang's apple version, which works as other clangs]