void* my_memmove(void* dest, const void* src, size_t num)
{
assert(dest && src);
void* ret = dest;
if ((char*)dest < (char*)src)//从前向后移
{
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
dest = (char*)dest + num;
src = (char*)src + num;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest - 1;
src = (char*)src - 1;//从后向前移
}
}
return ret;
}
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int i = 0;
memmove(arr+2, arr, 20);
my_memmove(arr, arr+2, 20);
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
struct s
{
char name[20];
int age;
};
void* my_memcpy(void* dest, const void * src, size_t num)
{
assert(dest && src);
void* ret = dest;
while (num--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest+1;//
src = (char*)src+1;//
}
return ret;
}
int main()
{
int arr1[] = { 1,2,3,4,5 };
int arr2[5] = { 0 };
struct s arr3[2] = { {"张三",20},{"李四",25} };
struct s arr4[3] = { 0 };
//memcpy(arr2, arr1, sizeof(arr1));//源头arr1的内存的大小
//vs环境下 memcpy 可以处理重叠拷贝的
my_memcpy(arr2, arr1, sizeof(arr1));//源头arr3的内存的大小
return 0;
}