Path: csiph.com!news.swapon.de!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: UB or not UB? was: On Undefined Behavior
Date: Tue, 03 Feb 2026 05:34:09 -0800
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <86v7gehrxq.fsf@linuxsc.com>
References: <10j6qdt$3q9n4$1@dont-email.me> <20260112162857.00003dd8@yahoo.com> <10k35mn$2ean4$1@dont-email.me> <20260112200821.000020a5@yahoo.com> <10k71vr$15aeb$11@dont-email.me> <10k7kga$3q0mm$2@dont-email.me> <87y0lzolxf.fsf@example.invalid> <10kagfd$n920$1@dont-email.me> <87ikd3nkui.fsf@example.invalid>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Tue, 03 Feb 2026 13:34:14 +0000 (UTC)
Injection-Info: dont-email.me; posting-host="72e789f1a61a439ff5daaffc25398413"; logging-data="1520319"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/F5Iuynkw8oGWTuGOVyTWjcrXXuaE3oEQ="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:TR+hUjok/uOZ5l3iOWut+g+hGF4= sha1:YOn3NNQeRcV/WXtbpy8Vj2VxZpI=
Xref: csiph.com comp.lang.c:396572
Keith Thompson writes:
> David Brown writes:
>
>> On 14/01/2026 23:43, Keith Thompson wrote:
>
> [...]
>
>>> There can be arbitrary padding between struct members, or after the
>>> last member. Almost(?) all implementations add padding only to
>>> satisfy alignment requirements, but the standard doesn't state any
>>> restrictions. There can be no padding before the first member, and
>>> offsets of members must be increasing.
>>
>> On closer reading, I agree with you here. I find it a little
>> surprising that this is not implementation-defined. If an
>> implementation can arbitrarily add extra padding within a struct, it
>> severely limits the use of structs in contexts outside the current
>> translation unit.
>
> In practice, struct layouts are (I think) typically specified by
> a system's ABI, and ABIs generally permit/require only whatever
> padding is necessary to meet alignment requirements.
>
> And I think C has rules about type compatibility that are intended to
> cover the same struct definition being used in different translation
> units within a program, though I'm too lazy to look up the details.
In fact, the rules in the C standard imply that any two struct
types that have members of the same types and in the same order
have the same layout (conceivably with different amounts of
padding at the end), regardless of whether the two struct types
are compatible or not.