4.5. memmove
void *memmove( void *v1, const void *v2, size_t size);memmove is the same as memcpy except that it works even if the objects overlap.
Consider the case of copying overlapping objects using a strcpy function which copies from the beginning of the string:
char str[80] = "abcdefg"; char *p = &str[2]; /* p points to the 'c' in str */strcpy( str, p) will work and str will contain "cdefg".
But strcpy( p, str) will not work properly.
If it did, you would expect str to end up as "ababcdefg".
But what happens is that the 'a' is copied into the 'c' position,
then 'b' on top of 'd',
then 'a' (now in the position of the original 'c') on top of 'e',
then 'b' ... etc. str ends up as:
"ababababab.... never stops!However, memmove( p, str, strlen(str)+1) will work as expected.
The +1 is needed so that the terminating zero byte will be copied.
If memcpy copies from the beginning of the object, then memmove can just call memcpy if v1 < v2. If v1 > v2, memmove has to do the copy starting from the end of v2 to the end of v1, copying the bytes in reverse order.