方法一
void* myMemcpy(void* dst, const void* src, size_t num)
{
if(NULL == dst || NULL == src)
return NULL;
char* pdst = (char*)dst;
const char* psrc = (const char*)src;
if(pdst >= psrc && pdst <= psrc + num - 1)
{
pdst = pdst + num - 1;
psrc = psrc + num - 1;
while(num--)
{
*pdst++ = *psrc++;
}
}
else
{
while(num--)
{
*pdst++ = *psrc++;
}
}
return pdst;
}
方法二
void myMemcpy(void* dst, const void* src, size_t num)
{
if(NULL == dst || NULL == src)
return NULL;
int wordNum = num / sizeof(int);
int slice = num % sizeof(int);
char* pdst = (char*)dst;
const char* psrc = (const char*)src;
if(pdst >= psrc && pdst <= psrc + nums - 1)
{
pdst = pdst + num - 1;
psrc = psrc + num - 1;
while(wordNum--)
{
*(int*)pdst-- = *(int*)psrc--;
}
while(slice--)
{
*pdst-- = *psrc--;
}
}
else
{
while(wordNum--)
{
*(int*)pdst++ = *(int*)psrc++;
}
while(slice--)
{
*pdst++ = *psrc++;
}
}
return pdst;
}