#include "stdafx.h"
#include <iostream>
#include <assert.h>
void* myMemMove(void *pDst, const void *pSrc, size_t iCount)
{
assert(NULL != pDst);
assert(NULL != pSrc);
char *pDT = (char*)pDst;
const char *pST = (const char*)pSrc;
if (pDT <= pST || pDT >= (pST + iCount)) //源地址和目的地址不重叠,低字节向高字节拷贝
{
while (iCount--)
{
*pDT++ = *pST++;
}
}
else //源地址和目的地址重叠,高字节向低字节拷贝
{
pDT = pDT + iCount - 1;
pST = pST + iCount - 1;
while (iCount--)
{
*pDT-- = *pST--;
}
}
return pDst;
}
int _tmain(int argc, _TCHAR* argv[])
{
char src[10] = "hellhelo";
char dst[10];
myMemMove(dst, src, strlen(src) + 1);
std::cout << dst;
getchar();
return 0;
}
C/C++--memmove函数实现
最新推荐文章于 2024-03-21 09:31:52 发布