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


Groups > comp.std.c > #6162

Re: Assignment between union object members of incompatible types

From Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups comp.std.c
Subject Re: Assignment between union object members of incompatible types
Date 2020-11-30 12:20 -0800
Organization None to speak of
Message-ID <87czzuoavh.fsf@nosuchdomain.example.com> (permalink)
References <272ec269-16e7-4e06-b8a8-e5f6534e66a8n@googlegroups.com>

Show all headers | View raw


Ian Abbott <ijabbott63@gmail.com> writes:
> A question (not posted by myself) from https://stackoverflow.com/questions/65077630 :
>
> Consider the following:
>
> union { int i; char c; } x = {0};
> x.c = x.i;
>
> Does the assignment x.c = x.i result in undefined behavior?
>
> C18 6.15.16.1/3 says:
>
> | If the value being stored in an object is read from another object 
> | that overlaps in any way the storage of the first object, then the 
> | overlap shall be exact and the two objects shall have qualified or 
> | unqualified versions of a compatible type; otherwise, the behavior is 
> | undefined.
>
> The objects x.c and x.i overlap, but have incompatible types, so on
> first glance it appears to be UB.

On first glance, yes, but I think this passage needs to be updated to
reflect its intent.

The RHS of an assignment is not an lvalue.  If it starts out as an
lvalue, then lvalue conversion is applied.  Logically the value is
retrieved *and then* copied into the destination object.

A simple case like this is not likely to cause problems (in the absence
of agressive optimization), but we can construct more problematic cases
where the object being copied is arbitrarily large and the overlap might
not be detectable at compile time.

I've written an answer:
https://stackoverflow.com/a/65080498/827263

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for Philips Healthcare
void Void(void) { Void(); } /* The recursive call of the void */

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


Thread

Assignment between union object members of incompatible types Ian Abbott <ijabbott63@gmail.com> - 2020-11-30 10:44 -0800
  Re: Assignment between union object members of incompatible types James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-11-30 14:45 -0500
  Re: Assignment between union object members of incompatible types Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-11-30 12:20 -0800
    Re: Assignment between union object members of incompatible types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-01 03:39 -0800
      Re: Assignment between union object members of incompatible types Francis Glassborow <francis.glassborow@btinternet.com> - 2020-12-05 14:27 +0000
        Re: Assignment between union object members of incompatible types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-07-10 08:42 -0700
  Re: Assignment between union object members of incompatible types Tim Rentsch <tr.17687@z991.linuxsc.com> - 2020-12-01 02:49 -0800

csiph-web