Re: Union C++ standard

gah4 <>
Fri, 26 Nov 2021 12:16:23 -0800 (PST)

          From comp.compilers

Related articles
Union C++ standard (Hans-Peter Diettrich) (2021-11-25)
Re: Union C++ standard (Kaz Kylheku) (2021-11-26)
Re: Union C++ standard (gah4) (2021-11-26)
Re: Union C++ standard (David Brown) (2021-11-27)
Re: Union C++ standard (Derek Jones) (2021-11-28)
Re: Union C++ standard (David Brown) (2021-11-28)
Re: Union C++ standard (Derek Jones) (2021-11-29)
Re: Union C++ standard (Kaz Kylheku) (2021-11-29)
Re: Union C++ standard (David Brown) (2021-11-29)
[4 later articles]
| List of all articles for this month |

From: gah4 <>
Newsgroups: comp.compilers
Date: Fri, 26 Nov 2021 12:16:23 -0800 (PST)
Organization: Compilers Central
References: 21-11-004
Injection-Info:; posting-host=""; logging-data="43064"; mail-complaints-to=""
Keywords: C, history, standards, comment
Posted-Date: 26 Nov 2021 21:15:22 EST
In-Reply-To: 21-11-004

On Friday, November 26, 2021 at 9:32:00 AM UTC-8, Hans-Peter Diettrich wrote:
> Can somebody explain why the access to members of a union is "undefined"
> except for the most recently written member?

> What can be undefined in a union of data types of the same typesize end
> alignment? Any member written will result in a unique bit/byte pattern
> in memory, whose reading may not make sense in a different type but
> undoubtedly is well defined.

In addition to the previously mentioned reasons, which I agree with,
there used to be (maybe still are) machines that tag memory with the
type stored. That makes it very difficult to access memory as bits of
a different type. Some language standards are written to allow for
those machines.

Many languages originated before IEEE floating point, where there
was no expectation that floating point values would agree between
different machines. Even more, some have "trap" values in floating
point, such that one can't reference some values. (VAX has a trap
value for negative zero.) Since the language can't control all this,
it is made undefined. (But otherwise, machine dependent.)

JVM, while not using tags, is defined such that programs don't
do that. The verifier is supposed to catch attempts, even if not
executed, to access memory the wrong way. Among others,
that allows for programs to be endian independent. (long takes
twice as much memory as int, but by refusing such access,
programs can't detect that, and so work on all hardware.)

(Not that it is likely that there will be a C++ compiler for JVM.)
[I think the Unisys Libra series may still run tagged Burroughs
architecture code, but if so I doubt there was ever a C compiler. -John]

Post a followup to this message

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