public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug web/53608] New: Documentation could be clearer about designated initializers of unions
@ 2012-06-08  5:05 alan.coopersmith at oracle dot com
  0 siblings, 0 replies; only message in thread
From: alan.coopersmith at oracle dot com @ 2012-06-08  5:05 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=53608

             Bug #: 53608
           Summary: Documentation could be clearer about designated
                    initializers of unions
    Classification: Unclassified
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: web
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: alan.coopersmith@oracle.com


http://gcc.gnu.org/onlinedocs/gcc/Designated-Inits.html currently says:

   If the same field is initialized multiple times, it will have value 
   from the last initialization.

(Which should probably say "the value" instead.)

Currently gcc applies this to multiple components of a union type as well.
(At least with gcc 3.4 through 4.5 here.)

For instance, this program:

#include <stdio.h>

int main(int argc, char **argv) {
    union {
    struct { int i; char c[4]; } a; 
    struct { int i; short s[2]; } b;
    } u = { .a.i = 1, .b.s[0] = 2, .b.s[1] = 3 };

    printf("%d %d %d\n", u.a.i, u.b.s[0], u.b.s[1]);
}

will print "0 2 3" when compiled with gcc, since gcc appears to treat
this as two initializations of the "same field" (fields mapped to the
same spot in memory):

     u.a = { 1 };
     u.b = { 0, 2, 3};

and discards the first.    (By comparison, the Solaris Studio compiler
prints "1 2 3" for this code.)

If the current behavior of gcc is considered correct, then it would be helpful 
to update the documentation to say something like:

   If the same field is initialized multiple times, or overlapping members
   of the same union are initialized, the value from the last initialization
   will be used.

   When union members are structures, the entire structure from the last
   member initialized is used.   For example:

    union {
    struct { int i; char c[4]; } a; 
    struct { int i; short s[2]; } b;
    } u = { .a.i = 1, .b.s[0] = 2, .b.s[1] = 3 };

   is equivalent to:

   union {
    struct { int i; char c[4]; } a; 
    struct { int i; short s[2]; } b;
    } u = { .a = { 1 }, .b = { 0, 2, 3 } };

   which is in turn equivalent to:

   union {
    struct { int i; char c[4]; } a; 
    struct { int i; short s[2]; } b;
    } u = { .b = { 0, 2, 3 } };


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2012-06-08  5:05 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-08  5:05 [Bug web/53608] New: Documentation could be clearer about designated initializers of unions alan.coopersmith at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).