memcpy的c实现:
//memcpy 实现::
void* my_memcpy(void* p1, const void* p2,size_t count)
{
char* tmp = p1;
count = count + 1;
while (count)
{
*(char*)p1 = *(char*)p2;
p1 = (char*)p1 + 1;
p2 = (char*)p2 + 1;
count--;
}
return tmp;
}
int main()
{
char arr1[30] = "********";
char arr2[] = "dada";
char* ret=my_memcpy(arr1, arr2,4);
printf("%s\n", ret);
system("pause");
return 0;
}
//memmove 模拟实现:
void*my_memmove(void* p1, void* p2, size_t count)
{
void* tmp = p1;
if (p1 <p2)
{
while (count--) //从前往后拷贝
{
*(char*)p1 = *(char*)p2;
p1 = (char*)p1 + 1;
p2 = (char*)p2 + 1;
}
}
else
{
while (count--)
{
*((char*)p1 + count) = *((char*)p2 + count); //从后往前拷贝
}
}
return tmp;
}
int main()
{
int arr1[] = { 1,2,3,4,5,6,7,8,9 };
my_memmove(arr1, arr1 + 2,4*sizeof(int));
for (int i = 0; i < 9; i++)
{
printf("%d ", arr1[i]);
}
/*my_memmove(arr1 + 1, arr1, 4 * sizeof(int));*/
system("pause");
return 0;
}