title: Why is Linux memmove() implemented the way it is?
body: <p>From the Linux manpage for <a href="http://linux.die.net/man/3/memmove" rel="noreferrer">memmove(3)</a></p>
  
  <blockquote>
    <p>The memmove() function copies n bytes from memory area src to memory area dest.
    The memory areas may overlap: copying takes place as though the bytes in src are
    first copied into a temporary array that does not overlap src or dest, and the
    bytes are then copied from the temporary array to dest.</p>
  </blockquote>
  
  <p>Instead of allocating a temporary array and copy the values twice we could just do the following:</p>
  
  <pre><code>void *my_memmove(void *dest, const void *src, size_t n) {
    signed char operation;
    size_t end;
    size_t current;
  
    if(dest != src) {
      if(dest &lt; src) {
        operation = 1;
        current = 0;
        end = n;
      } else {
        operation = -1;
        current = n - 1;
        end = -1;
      }
  
      for( ; current != end; current += operation) {
        *(((unsigned char*)dest) + current) = *(((unsigned char*)src) + current);
      }
    }
    return dest;
  }
  </code></pre>
  
  <p>In this implementation we simply take care of the position where we begin to copy. </p>
  
  <p>Is there a drawback in my implementation?</p>
  
  <p><em>Note: I won't actually use my implementation. I'm just curious.</em></p>