Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #398798
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: Are designated initializer supposed to zero padding? |
| Date | Mon, 11 May 2026 21:59:29 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 131 |
| Message-ID | <86zf25nrym.fsf@linuxsc.com> (permalink) |
| References | <10tqqso$kn23$1@dont-email.me> <86jytar6n2.fsf@linuxsc.com> <20260511232247.00006c5e@yahoo.com> <86wlx9pp10.fsf@linuxsc.com> <10ttnl3$1g54p$2@kst.eternal-september.org> <86lddppgze.fsf@linuxsc.com> <10ttvo6$1irrv$1@kst.eternal-september.org> |
| MIME-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Date | Tue, 12 May 2026 04:59:32 +0000 (UTC) |
| Injection-Info | dont-email.me; logging-data="1789499"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nSkqKT5s8j6hxYgehALfQeHiJje+izrY="; posting-host="714746643ff61f997ea29d517f63563d" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:5q2yhVLsA1ZPvz24pf+J9M2JxEg= sha1:G5i6jaXo3jNs43F4iR44T2AN8VE= sha256:/ksOgIK7azQe9sfO1dwSQ9zqgl+domjB8OFiE7LnuJs= sha1:FIzORR/tNIZt6rmop1avxvT6ilI= |
| Xref | csiph.com comp.lang.c:398798 |
Show key headers only | View raw
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>
>>>> Michael S <already5chosen@yahoo.com> writes:
>>>>
>>>>> On Sun, 10 May 2026 20:01:53 -0700
>>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> 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;
>>>>
>>>> The problem is padding is none of those things.
>>>
>>> Um, padding is padding. "... and any padding is initialized to zero
>>> bits".
>>
>> Sorry, I stand corrected. It looks like this change was made as
>> part of C11. So in C99 padding is not initialized to zeros, and
>> in C11 and later it is.
>>
>>> As I wrote elsethread, it seems clear that padding within subobjects
>>> (except for automatic objects with no initializer) is set to zero bits.
>>> I haven't found wording that applies that to top-level padding.
>>
>> What does it mean to talk about top-level padding? Isn't it the
>> case that padding (not counting padding bits in arithmetic types)
>> occurs only in structs and unions? Can you give an example of a
>> declaration where "top-level padding" occurs?
>>
>> I should add that I haven't yet read the other post where you
>> talk about this.
>
> By "top-level padding", I mean padding between members of the named
> object being initialized, as opposed to padding in subobjects.
> (Similar considerations apply to unions.)
>
> In my example in the other post, I have an outer struct with an
> inner struct as one of its members, and an object defined with an
> initializer that doesn't explicitly initialize all its members.
> I can derive from the wording in N3220 that any padding between
> members of the inner struct are set to zero bits, but not that this
> is done for padding between members in the outer struct.
Okay, I understand now what you're asking about. Just a couple of
small detours before getting to your question.
First, I have confirmed that the change to the C standard about
zeroing padding bits was made in C11, and was not present in C99.
Second, I agree with your conclusion that a contained struct or
union that has not been initialized explicitly must have any
padding set to all zero bits.
Third, as best I can determine, the rules under N3220 have the same
meaning as the corresponding rules in C11, of course modulo some
parts of C23 that were added after C11 and are not part of N1570.
Now for the question. Given this type definition:
typedef struct { struct { char c; short s; } inner; long k; } Outer;
and a subsequent declaration
Outer outer = { .k = 1 };
we agree that any padding in outer.inner will have been initialized
to all zero bits, but what about padding in outer (and outside of
outer.inner)?
My reading of N3220, which coincides with my reading of N1570, is
that the bytes of the object representation of outer that correspond
to padding bytes (of outer) take unspecified values. That result is
consistent with 6.2.6.1 paragraph 6, and I don't seen anything in
N3220's 6.7.11 (which is 6.7.9 in N1570) that overrides that or that
contradicts it.
I don't see anything in either standard that talks about padding
bits (as opposed to padding bytes) that occur as the result of
bitfields. Presumbly padding bits (in structs and unions) get the
same treatment as padding bytes: set to zero if their containing
struct or union is not initialized, and given unspecified values if
any member is initialized.
In C23, my understanding is "default initialization" is the same as
what initialization is done for static objects with no initializer;
which is to say, the declared (sub)object is initialized, but not
with an explicit initializer (and thus padding in structs or unions
with a default initializer must be set to zero).
I haven't tried to answer the question of what happens when a struct
or union is initialized as a whole, by assignment. For example, if
we have
typedef struct { struct { char c; short s; } inner; long k; } Outer;
Outer foo = (Outer){ .k = 1 };
I haven't tried to find out whether foo.inner might be allowed to
have non-zero padding bytes. I think an argument could be made
either way, but I haven't spent any time trying to really track it
down. In C99 it seems clear that any padding is unspecified, so
erring on the side of caution it's probably best to assume that.
Thank you for the correction upstream about rules for initializing
padding.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar
Are designated initializer supposed to zero padding? highcrew <high.crew3868@fastmail.com> - 2026-05-10 22:47 +0200
Re: Are designated initializer supposed to zero padding? James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-05-10 19:15 -0400
Re: Are designated initializer supposed to zero padding? highcrew <high.crew3868@fastmail.com> - 2026-05-11 09:11 +0200
Re: Are designated initializer supposed to zero padding? James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-05-12 16:02 -0400
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-12 21:10 -0700
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-13 15:51 -0700
Re: Are designated initializer supposed to zero padding? David Brown <david.brown@hesbynett.no> - 2026-05-14 11:36 +0200
Re: Are designated initializer supposed to zero padding? Richard Harnden <richard.nospam@gmail.invalid> - 2026-05-14 11:47 +0100
Re: Are designated initializer supposed to zero padding? David Brown <david.brown@hesbynett.no> - 2026-05-14 14:11 +0200
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-14 15:55 -0700
Re: Are designated initializer supposed to zero padding? David Brown <david.brown@hesbynett.no> - 2026-05-15 10:20 +0200
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-10 20:01 -0700
Re: Are designated initializer supposed to zero padding? highcrew <high.crew3868@fastmail.com> - 2026-05-11 09:10 +0200
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-12 09:36 -0700
Re: Are designated initializer supposed to zero padding? scott@slp53.sl.home (Scott Lurndal) - 2026-05-11 15:34 +0000
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-11 18:23 -0700
Re: Are designated initializer supposed to zero padding? Michael S <already5chosen@yahoo.com> - 2026-05-11 23:22 +0300
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-11 14:34 -0700
Re: Are designated initializer supposed to zero padding? Michael S <already5chosen@yahoo.com> - 2026-05-12 00:55 +0300
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-11 15:27 -0700
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-11 16:07 -0700
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-11 15:19 -0700
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-11 16:10 -0700
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-11 18:13 -0700
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-11 18:28 -0700
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-11 21:59 -0700
Re: Are designated initializer supposed to zero padding? David Brown <david.brown@hesbynett.no> - 2026-05-12 09:15 +0200
Re: Are designated initializer supposed to zero padding? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2026-05-12 00:27 -0700
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-12 06:44 -0700
Re: Are designated initializer supposed to zero padding? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-05-11 18:15 -0700
Re: Are designated initializer supposed to zero padding? Michael S <already5chosen@yahoo.com> - 2026-05-12 01:00 +0300
csiph-web