https://gcc.gnu.org/bugzilla/show_bug.cgi?id=63645

--- Comment #14 from M Welinder <terra at gnome dot org> ---
> 1) Your malloc is too small. It has to be sizeof (biggest member).
> So you're invoking undefined behavior.

Can you produce a specific authoritative reference for that statement?
I.e., a reference to the standard not "because I say so".  Note, that
the standard's Section 6.5 #7 says that I *can* accees my GnmExprBinary
object through the union.


> 2) In the if statement, where you probe the different members, you
> also invoke undefined behavior.

Absolutely not!  I went other that in painful detail in comment 8.
Bottom line: only one member is accessed.


> Pedantically speaking, the testcase is invalid I think, in C writing
> one union member invalidates all the other union members.

While true as written, there is a read-side exception to that which
allows reading the active union member's fields through certain
inactive union members' fields:

[#5] With one exception, if the value of a member of a union
       object  is used when the most recent store to the object was
       to    a    different     member,     the     behavior     is
       implementation-defined.70)  One special guarantee is made in
       order to simplify the use of unions:  If  a  union  contains
       several structures that share a common initial sequence (see
       below), and if the union object currently  contains  one  of
       these  structures,  it  is  permitted  to inspect the common
       initial part of any of them anywhere that a  declaration  of
       the  completed type of the union is visible.  Two structures
       share a common initial  sequence  if  corresponding  members
       have compatible types (and, for bit-fields, the same widths)
       for a sequence of one or more initial members.

Reply via email to