功能:
从起始地址开始,对目标进行拷贝。
注意:
memmove可以处理memcpy不能处理的内存重叠的情况。
memmove的模拟:
#include <stdio.h>
void* my_memmove(void* dest, void* str, size_t num)
{
char* cur = (char*)dest;
if (dest < str)
{
while (num--)
{
*((char*)dest) = *((char*)str);
dest = (char*)dest + 1;
str = (char*)str + 1;
}
}
else
{
while (num--)
{
*((char*)dest + num) = *((char*)str + num);
}
}
}
int main()
{
int a[] = { 0,1,2,3,4,5,6,7,8,9 };
my_memmove(a, a + 2, 20);
for (int i = 0; i <10; i++)
{
printf("%d", a[i]);
}
return 0;
}
运行结果: