Re: Union C++ standard

Kaz Kylheku <480-992-1380@kylheku.com>
Mon, 29 Nov 2021 16:39:03 -0000 (UTC)

          From comp.compilers

Related articles
Union C++ standard DrDiettrich1@netscape.net (Hans-Peter Diettrich) (2021-11-25)
Re: Union C++ standard 480-992-1380@kylheku.com (Kaz Kylheku) (2021-11-26)
Re: Union C++ standard gah4@u.washington.edu (gah4) (2021-11-26)
Re: Union C++ standard david.brown@hesbynett.no (David Brown) (2021-11-27)
Re: Union C++ standard derek@NOSPAM-knosof.co.uk (Derek Jones) (2021-11-28)
Re: Union C++ standard david.brown@hesbynett.no (David Brown) (2021-11-28)
Re: Union C++ standard derek@NOSPAM-knosof.co.uk (Derek Jones) (2021-11-29)
Re: Union C++ standard 480-992-1380@kylheku.com (Kaz Kylheku) (2021-11-29)
Re: Union C++ standard david.brown@hesbynett.no (David Brown) (2021-11-29)
Re: Union C++ standard Keith.S.Thompson+u@gmail.com (Keith Thompson) (2021-11-29)
Re: Union C++ standard derek@NOSPAM-knosof.co.uk (Derek Jones) (2021-11-30)
Re: Union C++ standard gneuner2@comcast.net (George Neuner) (2021-11-30)
Re: Union C++ standard david.brown@hesbynett.no (David Brown) (2021-11-30)
| List of all articles for this month |

From: Kaz Kylheku <480-992-1380@kylheku.com>
Newsgroups: comp.compilers
Date: Mon, 29 Nov 2021 16:39:03 -0000 (UTC)
Organization: A noiseless patient Spider
References: 21-11-004 21-11-008 21-11-009 21-11-010
Injection-Info: gal.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="38682"; mail-complaints-to="abuse@iecc.com"
Keywords: C, standards
Posted-Date: 29 Nov 2021 11:48:55 EST

On 2021-11-28, David Brown <david.brown@hesbynett.no> wrote:
> On 28/11/2021 13:51, Derek Jones wrote:
>> David,
>>
>>> In C, type-punning via unions is allowed (i.e., fully defined behaviour
>>
>> That is not true.  Writing into one member and then reading from
>> another member is undefined behavior.
>
> No, it is correct. It would be helpful if you looked at the full
> published standards, or (as most people do, since they are free) the
> final pre-publishing drafts. In particular, they contain the footnotes
> that appear to be missing in the format you linked here. Footnotes are
> not part of the normative text, but are added for clarification.


Not being normative means that anything that looks like a requirement
that is in a footnote is not actually a requirement.


Footnotes can only clarify requirements that are not themselves in a
foonote; they can't add new requirements.


If you cannot infer the existence of a requirement while ignoring all
foonotes and examples, it isn't there.


> These quotations are from C18 (draft N2346), which is the current C
> standard (until C23 is finalised). They have not changed since C99,
> when the footnote was added without a change to the normative text.


I have a copy of C99 (the final thing from ANSI, not a draft); I do not
see any such footnote; the paragraph has no footnotes.


> This means that as far as the C committee was concerned, using unions
> for type-punning has always (since standardisation) been valid in C,


Note that the "trap representation" terminology didn't exist prior to
C99, so any footnote referencing such a thing cannot possibly reflect
any intent about what C was going back to before standardization,
let alone some new footnote since C99.


> they realised that the text was unclear and thus added the footnote.
> (Arguably, since C90 did not clearly state that type-punning was
> defined, the behaviour was in fact undefined - though probably all C
> compilers allowed the behaviour.)


The concept of trap representations adds nuance to the requirements
for accessing objects; it doesn't make everything defined.


The trap concept is used to create a new (in C99) model why accessing an
object a field of "unsigned char" is okay: the unsigned char type has no
trap representation: all combinations of bit patterns give rise to a
valid value.


For instance, if we have a union like this:


    union u {
          int x;
          unsigned char y[sizeof (int)];
    }


then certain requirements can be inferred if we store x and access y[0],
based on knowing the implementation's parameters, like size and
representation of the integer and byte order.


I don't believe that the footnote you quoted gives any special blessing
to union-based type punning over pointer-based type punning. It just
clarifies the fact that unions are type punning, subject to the same
requirements as any other type punning. It points the reader to section
6.2.6 where the real requirements are, using which all instances of
type punning are to be interpreted.


--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.