[cfe-dev] question about initializing multiple members of unions

Gao, Yunzhong yunzhong_gao at playstation.sony.com
Tue Sep 17 13:49:55 PDT 2013


Hi Mattew,
I am fine with Richard's and Eli's approach.
- Gao.


> -----Original Message-----
> From: Matthew Curtis [mailto:mcurtis at codeaurora.org]
> Sent: Tuesday, September 17, 2013 6:37 AM
> To: Richard Smith
> Cc: Gao, Yunzhong; cfe-dev at cs.uiuc.edu Developers; Eli Friedman
> Subject: Re: [cfe-dev] question about initializing multiple members of unions
> 
> On 9/16/2013 1:05 PM, Richard Smith wrote:
>  > On Mon, Sep 16, 2013 at 7:11 AM, Matthew Curtis
> <mcurtis at codeaurora.org> wrote:
>  >
>  >     Richard, Doug,
>  >
>  >     Do either of you have an recommendation here?
>  >
>  >     Eli recommends GCC's behavior, while Philip Reames and Gao would
> both prefer the alternate behavior below (same as a series of assignments).
>  >
>  > Can you explain how treating the designated initializers as a sequence of
> assignments could give the object the value {0, 1, 2, 3}?
> 
> Given the following program :
> 
>      #include <stdio.h>
> 
>      typedef union {
>        struct {
>          int zero;
>          int one;
>          int two;
>          int three;
>        } a;
>        int b[4];
>      } my_agg_t;
> 
>      // set value using initializer
>      my_agg_t X = {
>        .b[0] = 0,
>        .a.one = 1,
>        .b[2] = 2,
>        .a.three = 3,
>      };
> 
>      int main(int argc, char *argv[])
>      {
> 
>        // set value using series of assignments
>        my_agg_t Y;
>        Y.b[0] = 0;
>        Y.a.one = 1;
>        Y.b[2] = 2;
>        Y.a.three = 3;
> 
>        printf("X:%d,%d,%d,%d\n", X.a.zero, X.a.one, X.a.two, X.a.three);
>        printf("Y:%d,%d,%d,%d\n", Y.a.zero, Y.a.one, Y.a.two, Y.a.three);
>      }
> 
> GCC Produces:
> 
>      X:0,0,0,3
>      Y:0,1,2,3
> 
> I believe Goa and Philip Reames are arguing that it seems very unintuitive
> that while the source code for setting X and setting Y look very similar they
> produce very different results.
> 
>  > I'm reasonably convinced that this is a hole in the C standard -- it doesn't
> specify what to do in this case -- and Eli's approach seems completely
> reasonable to me. Plus it has the benefit of > being compatible with at least
> two other major compilers (g++ and EDG), and doesn't require us to invent a
> new representation for initializer lists.
> 
> On 9/16/2013 5:28 PM, Richard Smith wrote:
>  > I still think Eli's approach is the right one here.
> 
> I agree.
> 
> Does this require further discussion or can I submit a patch that implements
> this behavior?
> 
> Thanks,
> Matthew
> 
> -- Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum,
> hosted by The Linux Foundation





More information about the cfe-dev mailing list