Re: [PATCH] ath9k: initialize arrays at compile time

From: Joe Perches
Date: Sun Mar 20 2022 - 13:38:48 EST


On Sun, 2022-03-20 at 18:17 +0100, Sebastian Gottschall wrote:
> Am 20.03.2022 um 17:48 schrieb John Crispin:
> >
> >
> > On 20.03.22 16:20, trix@xxxxxxxxxx wrote:
> > > array[size] = { 0 };
> >
> > should this not be array[size] = { }; ?!
> >
> > If I recall correctly { 0 } will only set the first element of the
> > struct/array to 0 and leave random data in all others elements
> >
> >     John
>
> You are right, john

No. The patch is fine.

Though generally the newer code in the kernel uses

type dec[size] = {};

to initialize stack arrays.

array stack declarations not using 0

$ git grep -P '^\t(?:\w++\s*){1,2}\[\s*\w+\s*\]\s*=\s*\{\s*\};' -- '*.c' | wc -l
213

array stack declarations using 0

$ git grep -P '^\t(?:\w++\s*){1,2}\[\s*\w+\s*\]\s*=\s*\{\s*0\s*\};' -- '*.c' | wc -l
776

Refer to the c standard section on initialization 6.7.8 subsections 19 and 21

19

The initialization shall occur in initializer list order, each initializer provided for a
particular subobject overriding any previously listed initializer for the same subobject
all subobjects that are not initialized explicitly shall be initialized implicitly the same as
objects that have static storage duration.

...

21

If there are fewer initializers in a brace-enclosed list than there are elements or members
of an aggregate, or fewer characters in a string literal used to initialize an array of known
size than there are elements in the array, the remainder of the aggregate shall be
initialized implicitly the same as objects that have static storage duration.