What is the difference between memmove and memcpy?

前端 未结 9 1707
Happy的楠姐
Happy的楠姐 2020-11-28 02:13

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

9条回答
  •  抹茶落季
    2020-11-28 02:34

    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.

提交回复
热议问题