strcpy()、 memcpy()、memmove()、memset()的实现
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 *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;
}
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;
}
图的上半部分为源内存区域在目标内存区域右边,下半部分为源内存区域在目标区域左边,源内存区域和目标内存区域都有交 叉。
memcpy()是从src的起始部分开始复制,所以虽然第一种情况下没有问题,但如果遇到第二种情况,则会发生错误, 如图所示,后两个字节在被复制前已经被覆盖掉了。而memmove()则由于采用了不同的复制机制,所以可以正确处理第二种情况。
void*Mymemcpy(void*dst,const void*src,size_t count)
{
ASSERT(dst!=NULL && src!=NULL);
//注意之所以转换为char*,是为了指针能够一位一位的操作,void*类型不定举个例子如果void* 为int*
//如果不加char*转换类型,那么dst+1指针位移将是4个字节
void *sret=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 sret;
}
源码:微软strcpy,strcat和strcmp的实现源代码 收藏
/***
*char *strcpy(dst, src) - copy one string over another
*
*Purpose:
* Copies the string src into the spot specified by
* dest; assumes enough room.
*
*Entry:
* char * dst - string over which "src" is to be copied
* const char * src - string to be copied over "dst"
*
*Exit:
* The address of "dst"
*
*Exceptions:
*******************************************************************************/
char* strcpy(char * dst, const char * src)
{
char * cp = dst;
while( *cp++ = *src++ )
; /* Copy src over dst */
return( dst );
}
/***
*char *strcat(dst, src) - concatenate (append) one string to another
*
*Purpose:
* Concatenates src onto the end of dest. Assumes enough
* space in dest.
*
*Entry:
* char *dst - string to which "src" is to be appended
* const char *src - string to be appended to the end of "dst"
*
*Exit:
* The address of "dst"
*
*Exceptions:
*
*******************************************************************************/
Char* strcat ( char * dst , const char * src )
{
char * cp = dst;
while( *cp )
cp++; /* find end of dst */
while( *cp++ = *src++ ) ; /* Copy src to end of dst */
return( dst ); /* return dst */
}
/***
*strcmp - compare two strings, returning less than, equal to, or greater than
*
*Purpose:
* STRCMP compares two strings and returns an integer
* to indicate whether the first is less than the second, the two are
* equal, or whether the first is greater than the second.
*
* Comparison is done byte by byte on an UNSIGNED basis, which is to
* say that Null (0) is less than any other character (1-255).
*
*Entry:
* const char * src - string for left-hand side of comparison
* const char * dst - string for right-hand side of comparison
*
*Exit:
* returns -1 if src < dst
* returns 0 if src == dst
* returns +1 if src > dst
*
*Exceptions:
*
*******************************************************************************/
int strcmp ( const char* src, const char* dst )
{
int ret = 0 ;
while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}