While I cannot state, that I am an ISO-expert, here is what I found out with the help of godbolt.
First I built the sample with the help of -fsanitize=undefined, which gives a good indication of undefined behavoir. Neither GCC nor clang were complaining.
Next I looked at the various stages gcc performs, in this case the gimple stage
foo ()
{
int a[5];
try
{
# DEBUG BEGIN_STMT
a = {};
a[2] = 1;
_1 = a[2];
a[0] = _1;
# DEBUG BEGIN_STMT
_2 = a[4];
_3 = a[3];
_4 = a[2];
_5 = a[1];
_6 = a[0];
printf ("%d %d %d %d %d\n", _6, _5, _4, _3, _2);
}
finally
{
a = {CLOBBER};
}
}
Here you can see, that first the array a
is defined, then 1
is assigned to a[2]
, afterwards that result (1
, because it is an assignment) is assigned to the first element of a
. The other in indices are left to 0 and therefore the pattern 1 0 1 0 0
is printed out.