What is the difference between memmove and memcpy?

ultraman picture ultraman · Jul 29, 2009 · Viewed 82.3k times · Source

What is the difference between memmove and memcpy? Which one do you usually use and how?

Answer

bdonlan picture bdonlan · Jul 29, 2009

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.