memmove函数实现
编写函数_memmove说明如下:
实现C 语言库函数memmove 的功能:将一块缓冲区中的数据移动到另一块缓冲区中。
void* _memmove(void* pDest, const void* pSrc, size_t count);
说明:不能直接调用memmove、memcpy 之类的库函数。
请详细点
[解决办法]
void * memmove(void * dest,const void *src,size_t count)
{
char *tmp, *s;
if (dest <= src) {
tmp = (char *) dest;
s = (char *) src;
while (count--)
*tmp++ = *s++;
}
else {
tmp = (char *) dest + count;
s = (char *) src + count;
while (count--)
*--tmp = *--s;
}
return dest;
}
[解决办法]
void* _memmove(void* pDest, const void* pSrc, size_t count)
{
int i = 0;
if ((!pDest) || (!pSrc))
{
return NULL;
}
if (pDest < pSrc)
{
for (i = 0; i < count; i++)
{
*((unsigned char *)pDest+i) = *((unsigned char *)pSrc+i);
}
}
else
{
for (i = count-1; i > = 0; i--)
{
*((unsigned char *)pDest+i) = *((unsigned char *)pSrc+i);
}
}
return pDest;
}
[解决办法]
贴个VC下的源码看看:
void * __cdecl memmove ( void * dst, const void * src, size_t count )
{
void * ret = dst;
if (dst <= src || (char *)dst > = ((char *)src + count)) {
/*
* Non-Overlapping Buffers
* copy from lower addresses to higher addresses
*/
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst + 1;
src = (char *)src + 1;
}
}
else {
/*
* Overlapping Buffers
* copy from higher addresses to lower addresses
*/
dst = (char *)dst + count - 1;
src = (char *)src + count - 1;
while (count--) {
*(char *)dst = *(char *)src;
dst = (char *)dst - 1;
src = (char *)src - 1;
}
}
return(ret);
}