title: Implementing own memcpy (size in bytes?)
body: <p>I recently had an interview question where I had to implement memcpy. I've used memcpy plenty in my experience so it didn't seem like a tough problem. </p>
  
  <p>So, I started implementing a loop to copy one address at a time from pointer to pointer, something like this:</p>
  
  <pre><code>void memcpy(void* dest, void* src, int size){
      for(int index = 0; index &lt; size; index++){
          dest[index] = src[index];
      }
  }
  </code></pre>
  
  <p>However the interviewers interrupted noting that the man page for memcpy says it "copies n bytes from src to dest" (which I confirmed later) and then wanted me to iterate instead by size/4 and then pick up the remaining with another loop of index &lt; size%4 (I guess assuming it was a 32 bit system?)</p>
  
  <p>Well, this seemed strange seeing as how I've used memcpy for years with no problem without having to give it a *4 modifier). When I got home I fired up gdb and copied a small string "hello" and was careful to input the size both with strlen() and constants to see where it starts and stops. </p>
  
  <pre><code>    char* src = "hello";
      char* dest = calloc(16, sizeof(char));
      int len = strlen(src);
      memcpy(dest, src, len); // both my version and official version
  </code></pre>
  
  <p>Now I carefully examined src and dest with gdb which both contained "hello\0". </p>
  
  <p>So my question is: what am I not understanding about using the number 4, (or "size in bytes")? And why does the documentation say "n bytes" when that's not really the behavior? What am I not seeing clearly here?</p>