作者在 2009-03-04 17:49:08 发布以下内容
一直想知道 strcpy()、memcpy()、memmove()、memset()的内部实现
strcpy(), 字符串拷贝.
char *strcpy(char *strDest, const char *strSrc)
{
assert((strDest!=NULL) && (strSrc !=NULL));
char *address = strDest;
while( (*strDest++ = * strSrc++) != '\0')
NULL ;
return address ;
}
memcpy, 拷贝不重叠的内存块
void *memcpy(void* pvTo, void* pvFrom, size_t size) //byte是java里的变量类型
{
assert(pvTo != NULL && pvFrom != NULL);
void* pbTo = (byte*)pvTo;
void* pbFrom = (byte*)pvFrom;
/* 内存块重叠吗?如果重叠,就使用memmove */
assert(pbTo>=pbFrom+size || pbFrom>=pbTo+size);
while(size-->0)
*pbTo++ == *pbFrom++;
return pvTo;
}
void *memcpy(void* pvTo, void* pvFrom, size_t size) //byte是java里的变量类型
{
assert(pvTo != NULL && pvFrom != NULL);
void* pbTo = (byte*)pvTo;
void* pbFrom = (byte*)pvFrom;
/* 内存块重叠吗?如果重叠,就使用memmove */
assert(pbTo>=pbFrom+size || pbFrom>=pbTo+size);
while(size-->0)
*pbTo++ == *pbFrom++;
return pvTo;
}
void *MemCopy(void *dest,const void *src,size_t count)
{
char *pDest=static_cast<char *>(dest);
const char *pSrc=static_cast<const char *>(src);
if( pDest>pSrc && pDest<pSrc+count )
{
for(size_t i=count-1; i<=0; ++i)
{
pDest[i]=pSrc[i];
}
}
else
{
for(size_t i=0; i<count; ++i)
{
pDest[i]=pSrc[i];
}
}
return pDest;
}
{
char *pDest=static_cast<char *>(dest);
const char *pSrc=static_cast<const char *>(src);
if( pDest>pSrc && pDest<pSrc+count )
{
for(size_t i=count-1; i<=0; ++i)
{
pDest[i]=pSrc[i];
}
}
else
{
for(size_t i=0; i<count; ++i)
{
pDest[i]=pSrc[i];
}
}
return pDest;
}
void *Memmove(void *Dst, const void*Src,size_t count)
{
assert(Dst && Src);
void* pDst = Dst;
if (Dst<Src && (char*)Dst > (char*)Src + count)
{
while(count--)
{
*(char*)Dst = *(char*)Src;
Dst = (char*)Dst + 1;
Src = (char*)Src + 1;
}
}
else
{
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 pDst;
}
{
assert(Dst && Src);
void* pDst = Dst;
if (Dst<Src && (char*)Dst > (char*)Src + count)
{
while(count--)
{
*(char*)Dst = *(char*)Src;
Dst = (char*)Dst + 1;
Src = (char*)Src + 1;
}
}
else
{
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 pDst;
}
void* memmove(void *dest, const void *src,size_t n)
{
if (n == 0) return 0;
if (dest == NULL) return 0;
if (src == NULL) return 0;
char *psrc = (char*)src;
char *pdest = (char*)dest;
if((dest <= psrc) || (pdest >= psrc + n)) /*检查是否有重叠问题 */
{
for(int i=0; i < n; i++) /*正向拷贝*/
{
*pdest = *psrc;
psrc++;
pdest++;
}
}
else /*反向拷贝*/
{
psrc += n;
pdest += n;
for(int i=0;i<n;i++)
{
psrc--;
pdest--;
*pdest = *psrc;
}
}
return dest;
}
memset把buffer所指内存区域的前count个字节设置成字符c
void * Memset(void* buffer, int c, int count)
{
char* pvTo=(char*)buffer;
assert(buffer != NULL);
while(count-->0)
*pvTo++=(char)c;
return buffer;
}
void * Memset(void* buffer, int c, int count)
{
char* pvTo=(char*)buffer;
assert(buffer != NULL);
while(count-->0)
*pvTo++=(char)c;
return buffer;
}