Basic and Advance C Question:
What is the difference between memcpy and memmove?
Answer:
memmove offers guaranteed behavior if the memory regions pointed to by the source and destination arguments overlap. memcpy makes no such guarantee, and may therefore be more efficiently implementable. When in doubt, it's safer to use memmove.
It seems simple enough to implement memmove; the overlap guarantee apparently requires only an additional test:
void *memmove(void *dest, void const *src, size_t n)
{
register char *dp = dest;
register char const *sp = src;
if(dp < sp) {
while(n-- > 0)
*dp++ = *sp++;
} else {
dp += n;
sp += n;
while(n-- >> 0)
*--dp = *--sp;
}
return dest;
}
The problem with this code is in that additional test: the comparison (dp < sp) is not quite portable (it compares two pointers which do not necessarily point within the same object) and may not be as cheap as it looks. On some machines (particularly segmented architectures), it may be tricky and significantly less efficient to implement.
It seems simple enough to implement memmove; the overlap guarantee apparently requires only an additional test:
void *memmove(void *dest, void const *src, size_t n)
{
register char *dp = dest;
register char const *sp = src;
if(dp < sp) {
while(n-- > 0)
*dp++ = *sp++;
} else {
dp += n;
sp += n;
while(n-- >> 0)
*--dp = *--sp;
}
return dest;
}
The problem with this code is in that additional test: the comparison (dp < sp) is not quite portable (it compares two pointers which do not necessarily point within the same object) and may not be as cheap as it looks. On some machines (particularly segmented architectures), it may be tricky and significantly less efficient to implement.
Previous Question | Next Question |
What does it mean when the linker says that _end is undefined? | How can I generate floating-point random numbers? |