Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #158479

Re: Usage of union

Path csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Usage of union
Date Wed, 20 Jan 2021 06:09:02 -0800
Organization A noiseless patient Spider
Lines 38
Message-ID <86pn1zog3l.fsf@linuxsc.com> (permalink)
References <0f20fca0-ccf0-4f43-99f7-d2ed20d9f3b7n@googlegroups.com> <ru1mk1$asv$1@dont-email.me>
Mime-Version 1.0
Content-Type text/plain; charset=us-ascii
Injection-Info reader02.eternal-september.org; posting-host="b1fd427021f152ca5ff6680f6ccee516"; logging-data="9471"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+Jpsh8vSlJhItAJMoKo6HRXdPPIFhYylI="
User-Agent Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock sha1:LXD6+7S430IF0ZCW/5xdPHe1yYU= sha1:P2oamR/pj0vQViFxHCuntphnVxY=
Xref csiph.com comp.lang.c:158479

Show key headers only | View raw


Andrey Tarasevich <andreytarasevich@hotmail.com> writes:

> On 1/17/2021 6:09 AM, wij wrote:
>
>> My understanding of union is from the low level point of view,
>> worked so far OK.  But confused again by reading the meaning of how
>> C++11 says it treats union.
>>
>> https://stackoverflow.com/questions/11373203/
>>     accessing-inactive-union-member-and-undefined-behavior
>>
>> //-----------------------------
>> union {
>>   uint16 u16;
>>   uint8  u8[2];
>>   float flt;
>> } u;
>>
>> void t() {
>>   u.u16=0;
>>
>>   assert(u.u8[0]==0);  // using u8 member is UB
>>   assert(u.u8[1]==0);  // then, what does UB mean?
>>                        // might the lines above trigger
>>                        //    Undefined Behavior?
>> };
>> //-----------------------------
>>
>> If I store values via member u16, surely I CAN use other members.
>
> [Briefly, C defines the behavior, C++ makes the behavior undefined
>  in all cases.]
>
> In the above example you are trying to use union for "type punning",
> i.e for memory reinterpretation.  This is not exactly what unions are
> for.

It is exactly one of the uses that unions are for.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Usage of union wij <wyniijj@gmail.com> - 2021-01-17 06:09 -0800
  Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-17 06:18 -0800
    Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 10:39 -0500
      Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-17 08:41 -0800
        Re: Usage of union Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-01-17 17:07 +0000
          Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:29 -0500
            Re: Usage of union Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2021-01-17 17:47 +0000
            Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 14:22 -0500
          Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-20 07:03 -0800
            Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-20 09:33 -0800
              Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-23 08:01 -0800
              Re: Usage of union Jorgen Grahn <grahn+nntp@snipabacken.se> - 2021-01-26 20:28 +0000
                Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-27 03:15 -0800
        Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:19 -0500
      Re: Usage of union Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-20 19:24 +0100
        Re: Usage of union Christian Hanné <the.hanne@gmail.com> - 2021-01-20 19:31 +0100
  Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-17 07:54 -0800
    Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-17 08:17 -0800
      Re: Usage of union wij <wyniijj@gmail.com> - 2021-01-17 09:07 -0800
      Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:32 -0500
      Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-20 05:56 -0800
        Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-20 09:52 -0800
    Re: Usage of union Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-01-20 06:09 -0800
      Re: Usage of union Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-20 19:41 +0100
        Re: Usage of union scott@slp53.sl.home (Scott Lurndal) - 2021-01-20 18:49 +0000
          Re: Usage of union Bonita Montero <Bonita.Montero@gmail.com> - 2021-01-20 20:09 +0100
            Re: Usage of union scott@slp53.sl.home (Scott Lurndal) - 2021-01-20 19:25 +0000
            Re: Usage of union David Brown <david.brown@hesbynett.no> - 2021-01-20 20:29 +0100
        Re: Usage of union Andrey Tarasevich <andreytarasevich@hotmail.com> - 2021-01-20 11:36 -0800
  Re: Usage of union James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-01-17 12:14 -0500
  Re: Usage of union Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-01-17 17:17 +0000

csiph-web