Jos,
That's where I am confused about , I could'nt make out why the instruction could not handle overlapping areas. I mean what it does to copy the memory right way without reversing the order of copy.
Thanks in advance,
Gsi.
Think about it: a single machine code instruction that can copy n bytes from A
to B, e.g. n == 3, A == 1 and B ==2 so copy byte from 1 to 2, copy byte from 2
to 3 and bingo: you're copying the original byte that was at position 1 originally.
A single instruction may be extremely fast but it is way too stupid to decide what
to do in case of overlapping areas. Hence the two functions: a stupid but fast one
and a clever but slow one. For the non-overlapping areas the memmove tries to
be as fast as possible. Some processors even have a descending copy instruction
so all the memmove function has to do is decide which one to use and fire up one
of the instructions. But still it'll be (a bit) slower than the memcpy function.
kind regards,
Jos