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.
So, I started implementing a loop to copy one address at a time from pointer to pointer, something like this:
void memcpy(void* dest, void* src, int size){
for(int index = 0; index < size; index++){
dest[index] = src[index];
}
}
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 < size%4 (I guess assuming it was a 32 bit system?)
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.
char* src = "hello";
char* dest = calloc(16, sizeof(char));
int len = strlen(src);
memcpy(dest, src, len); // both my version and official version
Now I carefully examined src and dest with gdb which both contained "hello\0".
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?