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?
Memcpy copies data bytes by byte from the source array to the destination array. This copying of data is threadsafe. The process of copying data can fail if the given size is not accurate for the destination array. At some point, the behaviors of the memcpy() function become undefined depending on the condition.
How to implement your own memcpy implementation in C? Implementation of memcpy is not a big deal, you need to typecast the given source and destination address to char* (1 byte). After the typecasting copy the data from the source to destination one by one until n (given length).
memcpy() in C/C++ // Copies "numBytes" bytes from address "from" to address "to" void * memcpy(void *to, const void *from, size_t numBytes);
memcpy() function in C/C++ The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string. h” header file in C language.
As others have said, copying 4 bytes at a time is faster than copying 1 byte at a time. The interviewer wanted you to do something like this:
void memcpy(void* dest, void* src, int size)
{
uint8_t *pdest = (uint8_t*) dest;
uint8_t *psrc = (uint8_t*) src;
int loops = (size / sizeof(uint32_t));
for(int index = 0; index < loops; ++index)
{
*((uint32_t*)pdest) = *((uint32_t*)psrc);
pdest += sizeof(uint32_t);
psrc += sizeof(uint32_t);
}
loops = (size % sizeof(uint32_t));
for (int index = 0; index < loops; ++index)
{
*pdest = *psrc;
++pdest;
++psrc;
}
}
They were asking you to optimize your implementation and have it copy a 32-bit word at a time inside the loop vs. a byte at a time. This would necessitate some careful checking to handle the boundary cases, such as size
not being a multiple of 4, or dest
or src
not being aligned on a 4-byte boundary.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With