/************************************************************************************1.模拟实现memmove函数的实现。(考虑内存重叠)
************************************************************************************/
#include<stdio.h>
#include<string.h>
#include<assert.h>
void * my_memmove(void *aim,const void *source,int length)
{
int i = 0;
void *p = aim;
assert(aim&&source);
if (((char*)aim - (char*)source) >= 0)//内存重叠从后往前拷贝
{
for (i = length-1; i >=0; i--)
{
*((char *)aim + i*sizeof(char)) = *((char *)source + i*sizeof(char));
}
return (p);
}
else //不重叠 正常拷贝
{
for (i = 0; i <length; i++)
{
*((char *)aim + i*sizeof(char)) = *((char *)source + i*sizeof(char));
}
return (p);
}
}
// 1 2 3 4 5 6 7 8 9 0 0
// *0*1*2*3*4*5*6*7*8*9*10
//1 2 3 4 5 6 7 8 9 0 0
int main()
{
int arr[11] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
int i = 0;
my_memmove(arr, arr , 36);
for (i; i < 11; i++)
printf("%d\n",arr[i]);
/*for (i = 0; i < 10;i++)
printf("%d\n",*( (int *)my_memmove(arr, arr+1, 36)+1));*/
return 0;
}
转载于:https://blog.51cto.com/shaungqiran/1683111