void *memcpy( void *to, const void *from, size_t count );
The function memcpy() copies count characters from the array from to the array to. The return value of memcpy() is to. The behavior of memcpy() is undefined if to and from overlap.
//==========================
void *memmove( void *to, const void *from, size_t count );
The memmove() function is identical to memcpy(), except that
it
works even if to and from overlap.
memcpy要进行copy的的两个
内存
区不能重叠
即to和from指向的内存不能交叉
memmove则允许重叠
memcpy没有考虑内存重叠的情况,所以在内存重叠的情况下可能出错;
memmove考虑了内存重叠的情况,所以在内存重叠的情况下不会出错;
二者的
效率
相比相差无几。
看了一眼memcpy和memmove的实现。
memmove多了个if语句:
if (dst <= src || (char *)dst >= ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}