Using memcpy() when source and destination overlap can lead to undefined behaviour – in those cases only memmove() can be used.
But what if I know for sure buffers don’t overlap – is there a reason to use specifically memcpy() or specifically memmove()? Which should I use and why?
Assuming a sane library implementor,
memcpywill always be at least as fast asmemmove. However, on most platforms the difference will be minimal, and on many platformsmemcpyis just an alias formemmoveto support legacy code that (incorrectly) callsmemcpyon overlapping buffers.Both
memcpyandmemmoveshould be written to take advantage of the fastest loads and stores available on the platform.To answer your question: you should use the one that is semantically correct. If you can guarantee that the buffers do not overlap, you should use
memcpy. If you cannot guarantee that the buffers don’t overlap, you should usememmove.