title: memcpy vs assignment in C -- should be memmove?
body: <p>As pointed out in <a href="https://stackoverflow.com/questions/324011/memcpy-vs-assignment-in-c">an answer to this question</a>, the compiler (in this case gcc-4.1.2, yes it's old, no I can't change it) can replace struct assignments with memcpy where it thinks it is appropriate.</p>
  
  <p>I'm running some code under valgrind and got a warning about memcpy source/destination overlap. When I look at the code, I see this (paraphrasing):</p>
  
  <pre><code>struct outer
  {
      struct inner i;
      // lots of other stuff
  };
  
  struct inner
  {
      int x;
      // lots of other stuff
  };
  
  void frob(struct inner* i, struct outer* o)
  {
      o-&gt;i = *i;
  }
  
  int main()
  {
      struct outer o;
  
      // assign a bunch of fields in o-&gt;i...
  
      frob(&amp;o.i, o);
      return 0;
  }
  </code></pre>
  
  <p>If gcc decides to replace that assignment with <code>memcpy</code>, then it's an invalid call because the source and dest overlap.</p>
  
  <p>Obviously, if I change the assignment statement in <code>frob</code> to call <code>memmove</code> instead, then the problem goes away.</p>
  
  <p>But is this a compiler bug, or is that assignment statement somehow invalid?</p>