Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c++
Subject: Re: Don't be fooled by cpp.sh
Date: Fri, 10 Jan 2020 21:26:38 -0800
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <86pnfq36nl.fsf@linuxsc.com>
References: <70391932-ccbc-4bc7-b693-881ab06eb6d3@googlegroups.com> <79fbc109-a9d8-47a2-a150-a74b09de4d75@googlegroups.com> <20191228210102.7a79a100573646105f604b46@cvine--nospam--.freeserve.co.uk> <7550eec0-8b46-40d9-8903-5bee38ebb37e@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: reader02.eternal-september.org; posting-host="a658bb27d1b88b897660621da5fe7a9e"; logging-data="25741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+0Ru7vqT7AnVLA8Er4wR1aWZGbH7xGYm8="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:JjtuigndD3/mb54J88SRBazOVUI= sha1:iKyZKPLxv597eA5JsipnRkAuNos=
Xref: csiph.com comp.lang.c++:71275
Daniel writes:
> On Sunday, December 29, 2019 at 1:30:50 PM UTC-5, bol...@nowhere.org wrote:
>
>> The whole point of a union is not to save memory, its to write as
>> one type and read as another
>
> Not according to K&R, 1st edition. K&R write
>
> "This is the purpose of a union ? to provide a single variable which
> can legitimately hold any one of several types.
>
> union u_tag {
>
> int ival;
> float fval;
> char *pval;
> } uval;
>
> The variable uval will be large enough to hold the largest of the
> three types, regardless of the machine it is compiled on ? the code
> is independent of hardware characteristics. Any one of these types
> may be assigned to uval and then used in expressions, so long as the
> usage is consistent: the type retrieved must be the type most
> recently stored. It is the responsibility of the programmer to keep
> track of what type is currently stored in a union; the results are
> machine dependent if something is stored as one type and extracted
> as another."
As far as how unions behave goes, what this says is basically the
same as what C89/C90 says, which in turn has the same intended
meaning as the more complete descriptions in later C standards.
Note that I'm not saying anything about the main or intended purpose
of unions. Clearly though K&R recognized that unions could be used
for type punning (as it is called today).