memcpy适用于不重叠场景
void* my_memcpy(void * dest, void * src, size_t count)
{
void * addr = dest;
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
return addr;
}
int arr1[] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[20] = { 0 };
my_memcpy(arr2, arr1, 40);
for (int i = 0; i < 20; i++)
{
printf("%d ", arr2[i]);
}
memmove适用于重叠场景(画图,区分前后拷贝顺序)
void* my_memmove(void * dest, const void * src, size_t count)
{
void * addr = dest;
if (dest < src)
{
//前--->后
while (count--)
{
*(char*)dest = *(char*)src;
dest = (char*)dest + 1;
src = (char*)src + 1;
}
}
else
{
//后--->前
while (count--)
{
*((char*)dest + count) = *((char*)src + count);
}
}
return addr;
}