What is the difference between memmove and memcpy?
With memcpy
, the destination cannot overlap the source at all. With memmove
it can. This means that memmove
might be very slightly slower than memcpy
, as it cannot make the same assumptions.
For example, memcpy
might always copy addresses from low to high. If the destination overlaps after the source, this means some addresses will be overwritten before copied. memmove
would detect this and copy in the other direction - from high to low - in this case. However, checking this and switching to another (possibly less efficient) algorithm takes time.
memmove
can handle overlapping memory, memcpy
can't.
Consider
char[] str = "foo-bar";
memcpy(&str[3],&str[4],4); //might blow up
Obviously the source and destination now overlap, we're overwriting
"-bar" with "bar". It's undefined behavior using memcpy
if the source
and destination overlap so in this case cases we need memmove
.
memmove(&str[3],&str[4],4); //fine
From the memcpy man page.
The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas should not overlap. Use memmove(3) if the memory areas do overlap.
Assuming you would have to implement both, the implementation could look like that:
void memmove ( void * dst, const void * src, size_t count ) {
if ((uintptr_t)src < (uintptr_t)dst) {
// Copy from back to front
} else if ((uintptr_t)dst < (uintptr_t)src) {
// Copy from front to back
}
}
void memcpy ( void * dst, const void * src, size_t count ) {
if ((uintptr_t)src != (uintptr_t)dst) {
// Copy in any way you want
}
}
And this should pretty well explain the difference. memmove
always copies in such a way, that it is still safe if src
and dst
overlap, whereas memcpy
just doesn't care as the documentation says when using memcpy
, the two memory areas must not overlap.
E.g. if memcpy
copies "front to back" and the memory blocks are aligned as this
[---- src ----]
[---- dst ---]
copying the first byte of src
to dst
already destroys the content of the last bytes of src
before these have been copied. Only copying "back to front" will lead to correct results.
Now swap src
and dst
:
[---- dst ----]
[---- src ---]
In that case it's only safe to copy "front to back" as copying "back to front" would destroy src
near its front already when copying the first byte.
You may have noticed that the memmove
implementation above doesn't even test if they actually do overlap, it just checks their relative positions, but that alone will make the copy safe. As memcpy
usually uses the fastest way possible to copy memory on any system, memmove
is usually rather implemented as:
void memmove ( void * dst, const void * src, size_t count ) {
if ((uintptr_t)src < (uintptr_t)dst
&& (uintptr_t)src + count > (uintptr_t)dst
) {
// Copy from back to front
} else if ((uintptr_t)dst < (uintptr_t)src
&& (uintptr_t)dst + count > (uintptr_t)src
) {
// Copy from front to back
} else {
// They don't overlap for sure
memcpy(dst, src, count);
}
}
Sometimes, if memcpy
always copies "front to back" or "back to front", memmove
may also use memcpy
in one of the overlapping cases but memcpy
may even copy in a different way depending on how the data is aligned and/or how much data is to be copied, so even if you tested how memcpy
copies on your system, you cannot rely on that test result to be always correct.
What does that mean for you when deciding which one to call?
-
Unless you know for sure that
src
anddst
don't overlap, callmemmove
as it will always lead to correct results and is usually as fast as that is possible for the copy case you require. -
If you know for sure that
src
anddst
don't overlap, callmemcpy
as it won't matter which one you call for the result, both will work correctly in that case, butmemmove
will never be faster thanmemcpy
and if you are unlucky, it may even be slower, so you can only win callingmemcpy
.