Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Are designated initializer supposed to zero padding?
Date: Mon, 11 May 2026 15:27:11 -0700
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <86pl31poow.fsf@linuxsc.com>
References: <10tqqso$kn23$1@dont-email.me> <86jytar6n2.fsf@linuxsc.com> <20260511232247.00006c5e@yahoo.com> <10tti1a$1eenk$4@kst.eternal-september.org> <20260512005524.000052a9@yahoo.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Mon, 11 May 2026 22:27:12 +0000 (UTC)
Injection-Info: dont-email.me; logging-data="1569546"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19pIyr5knFaJrLLcEDJOhSGOkukIF3GW60="; posting-host="e12ff559fd79fb3307443db8d47ea871"
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:wZCHuAkrh+h5c/QYRoJldss87c4= sha1:T589VcAPkD85HIp8oz1mHGnHEg8= sha256:6N+P/DVyuXqXhVNyQi2dNFCGwFe5h/QE+DYc0gveNdE= sha1:6r+OOLfxTd0iL0XivFRZAi4P2is=
Xref: csiph.com comp.lang.c:398784
Michael S writes:
> On Mon, 11 May 2026 14:34:34 -0700
> Keith Thompson wrote:
>
>> Michael S writes:
>>
>>> On Sun, 10 May 2026 20:01:53 -0700
>>> Tim Rentsch wrote:
>>>
>>>> Point 1: initializers are not required to set padding (either
>>>> padding bits or padding bytes). Don't expect padding to be
>>>> zeroed. This statement applies to initializers in all forms -
>>>> regular initializers, designated initializers, and compound
>>>> literals.
>>>
>>> James Kuyper says that zeroing of padding is required by that
>>> standard. I am not an expert in lawyer-style reading of the
>>> standard, but at my level it looks that he is correct and the
>>> wording in unequivocal. For example, n3220, 6.7.11:
>>>
>>> 11
>>> If an object that has automatic storage duration is not initialized
>>> explicitly, its representation is indeterminate. If an object that
>>> has static or thread storage duration is not initialized
>>> explicitly, or any object is initialized with an empty initializer,
>>> then it is subject to default initialization, which initializes an
>>> object as follows: ? if it has pointer type, it is initialized to a
>>> null pointer; ? if it has decimal floating type, it is initialized
>>> to positive zero, and the quantum exponent is
>>> implementation-defined; ? if it has arithmetic type, and it does
>>> not have decimal floating type, it is initialized to (positive or
>>> unsigned) zero; ? if it is an aggregate, every member is
>>> initialized (recursively) according to these rules, and any padding
>>> is initialized to zero bits;
>>
>> That applies only to objects with static storage duration *without*
>> an initializer (or with an empty initializer {}, a new feature
>> in C23). It doesn't imply that if there is an initializer, padding
>> is zeroed.
>>
>> If you want to set *some* members to non-zero values and guarantee
>> zero for other members and all-bits-zero for padding, there's
>> no direct way to do it. You could initialize an object with {}
>> (or {0} pre-C23) and then assign values to the desired members.
>
> In this draft it is less clear. Let's look at N2310,6.7.9:
>
> On one hand, we have 10+19:
>
> If an object that has automatic storage duration is not initialized
> explicitly, its value is indeterminate.
> If an object that has static or thread storage duration is not
> initialized explicitly, then:
> ? if it has pointer type, it is initialized to a null pointer;
> ? if it has arithmetic type, it is initialized to (positive or
> unsigned) zero;
> ? if it is an aggregate, every member is initialized (recursively)
> according to these rules, and any padding is initialized to zero bits;
> ? if it is a union, the first named member is initialized (recursively)
> according to these rules, and any padding is initialized to zero bits;
>
>
> 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.
>
> It sounds like zeroing of padding is required.
>
>
> On the other hand, we have 9:
> Except where explicitly stated otherwise, for the purposes of this
> subclause unnamed members of objects of structure and union type do not
> participate in initialization. Unnamed members of structure objects
> have indeterminate value even after initialization.
>
> It sounds like zeroing of padding is not required.
>
> It's quite confusing.
Padding and unnamed members are different things. All members,
including unnamed members, have a type. Padding doesn't have
a type.
See also my other response.