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

            Bug ID: 105343
           Summary: Inefficient initialisation in some kinds of structs
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: david at westcontrol dot com
  Target Milestone: ---

struct S { int a[1000]; };
struct X { struct S s; int b[2];};

extern int foobar(struct X * p);

int foo(struct S *s)
{
    struct X x = { *s };
    return foobar(&x);
}


When the size of the array "a" is small enough that the compiler does the
initialisation inline, the code is fine.  With a bigger array it uses memset
and memcpy, either as calls to external functions or inline loops depending on
details of the version of gcc and the target.  (This too is appropriate.)

However, it does that by turning the code into the equivalent of :

    memset(&x, 0, sizeof(struct X));
    memcpy(&x, s, sizeof(struct S));

It /should/ be doing :

    memset(&x.b, 0, sizeof(struct X.b));
    memcpy(&x, s, sizeof(struct S));

In other words, it is first zeroing out the entire X structure, then copying
from *s into the structure.  Only the extra part of X, the array "b", needs to
be zero'ed.

Reply via email to