title: Is it legal and well defined behavior to use a union for conversion between two structs with a common initial sequence (see example)?
body: <p>I have an API with a publicly facing struct A and an internal struct B and need to be able to convert a struct B into a struct A. Is the following code legal <strong>and well defined behavior</strong> in C99 (and VS 2010/C89) and C++03/C++11? If it is, please explain what makes it well-defined. If it's not, what is the most efficient and cross-platform means for converting between the two structs?</p>
  
  <pre><code>struct A {
    uint32_t x;
    uint32_t y;
    uint32_t z;
  };
  
  struct B {
    uint32_t x;
    uint32_t y;
    uint32_t z;
    uint64_t c;
  };
  
  union U {
    struct A a;
    struct B b;
  };
  
  int main(int argc, char* argv[]) {
    U u;
    u.b.x = 1;
    u.b.y = 2;
    u.b.z = 3;
    u.b.c = 64;
  
    /* Is it legal and well defined behavior when accessing the non-write member of a union in this case? */
    DoSomething(u.a.x, u.a.y, u.a.z);
  
    return 0;
  }
  </code></pre>
  
  <p><br>
  <strong>UPDATE</strong></p>
  
  <p>I simplified the example and wrote two different applications. One based on memcpy and the other using a union.</p>
  
  <p><br>
  <em>Union:</em></p>
  
  <pre><code>struct A {
    int x;
    int y;
    int z;
  };
  
  struct B {
    int x;
    int y;
    int z;
    long c;
  };
  
  union U {
    struct A a;
    struct B b;
  };
  
  int main(int argc, char* argv[]) {
    U u;
    u.b.x = 1;
    u.b.y = 2;
    u.b.z = 3;
    u.b.c = 64;
    const A* a = &amp;u.a;
    return 0;
  }
  </code></pre>
  
  <p><br>
  <em>memcpy:</em></p>
  
  <pre><code>#include &lt;string.h&gt;
  
  struct A {
    int x;
    int y;
    int z;
  };
  
  struct B {
    int x;
    int y;
    int z;
    long c;
  };
  
  int main(int argc, char* argv[]) {
    B b;
    b.x = 1;
    b.y = 2;
    b.z = 3;
    b.c = 64;
    A a;
    memcpy(&amp;a, &amp;b, sizeof(a));
    return 0;
  }
  </code></pre>
  
  <p><br><br>
  <em>Profiled Assembly [DEBUG] (Xcode 6.4, default C++ compiler):</em></p>
  
  <p>Here is the relevant difference in the assembly for debug mode. When I profiled the release builds there was no difference in the assembly.</p>
  
  <p><br>
  Union:</p>
  
  <pre><code>movq     %rcx, -48(%rbp)
  </code></pre>
  
  <p><br>
  memcpy:</p>
  
  <pre><code>movq    -40(%rbp), %rsi
  movq    %rsi, -56(%rbp)
  movl    -32(%rbp), %edi
  movl    %edi, -48(%rbp)
  </code></pre>
  
  <p><br><br>
  <em>Caveat:</em></p>
  
  <p>The example code based on union produces a warning regarding variable 'a' being unused. As the profiled assembly is from debug, I don't know if there is any impact.</p>