From http://en.cppreference.com/w/cpp/string/byte/memmove
Despite being specified "as if" a temporary buffer is used, actual implementations of this function do not incur the overhead of double copying or extra memory. For small count, it may load up and write out registers; for larger blocks, a common approach (glibc and bsd libc) is to copy bytes forwards from the beginning of the buffer if the destination starts before the source, and backwards from the end otherwise, with a fall back to std::memcpy when there is no overlap at all.
Therefore the overhead in all likelihood is a couple of conditional branches. Hardly worth worrying about for large blocks.
However, it is worth remembering that std::memcpy
is a 'magic' function, being the only legal way to cast between two dissimilar types.
In c++, this is illegal (undefined behaviour):
union {
float a;
int b;
} u;
u.a = 10.0;
int x = u.b;
This is legal:
float a = 10.0;
int b;
std::memcpy(std::addressof(b), std::addressof(a), size(b));
and does what you'd expect the union to do if you were a C programmer.
memmove
might be slower thanmemcpy
is because it is able to handle overlapping memory, butmemmove
still only copies the data once. – Some programmer dude Jul 7 '17 at 8:41memmove
of your platform is most likely already highly optimized, so just usememmove
if source and destination may overlap and don't bother. If you are sure that source and destination will never overlap, then just usememcpy
. – Jabberwocky Jul 7 '17 at 8:46