Re: [PATCH 02/11] submodule--helper: replace memset() with { 0 }-initialization
- From
Glen Choo <chooglen@google.com>
- Date
- Jul 13, 2022, 22:41 UTC
- Message-ID
- <kl6lbkts63fb.fsf@chooglen-macbookpro.roam.corp.google.com>
- In-Reply-To
- <xmqqy1wwaey8.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> writes:
Show 18 quoted lines
> Glen Choo <chooglen@google.com> writes:
>
>> Ok. I wonder if we could reduce this kind of churn in the future by
>> adding this to CodingGuidelines, e.g. "always use { 0 } for stack
>> variables". Tangentially, do we require { NULL } when the first element
>> is a pointer? (I'm not sure because this isn't in CodingGuidelines
>> either AFAICT.)
>
> A valiable can legitimately be left uninitialized, or initialized to
> a known value that is not zero using designated initializers. So
> saying something like
>
> When zero-initializing an auto variable that is a struct or
> union in its definition, use "{ 0 }", whether the first member
> in the struct is of a number, a pointer, or a compound type.
>
> may be OK. I do not think we would want to say "always use X", as
> the world is not that simple..Thanks. Also, I made a typo here, I meant to be more specific with regards to memset(), like:
When zero-initializing an auto variable that is a struct or
union in its definition, use "{ 0 }", whether the first member
in the struct is of a number, a pointer, or a compound type. _Do not
use memset()._Unless there really is a legitimate reason to use memset(&my_auto_var, 0 sizeof(my_autovar)) that I've missed?
Show 9 quoted lines
> We do favor designated initializers over traditional initialization > in the order of members these days, so something like > > When declaring a struct/union variable or an array with initial > value to some members or elements, consider using designated > initializers, instead of listing the values in the order of > members in the definition of the struct. > > would also be good.
Thanks. If I have time I'll send that proposal to CodingGuidelines, or someone else can send it (I don't mind either way).