全部博文(930)
分类: LINUX
2009-09-28 17:06:21
之前的string.h我已经写过字符串的了.今天就谢谢mem的吧.
memcpy, 拷贝不重叠的内存块
void *memcpy(void* pvTo, void* pvFrom, size_t size)
{
assert(pvTo != NULL && pvFrom != NULL);
char* pbTo = (char*)pvTo;
char* pbFrom = (char*)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);
char* pDst = (char*)Dst;
if (Dst
{
while(count--)
*Dst++ = *Src++;
}
else
{
Dst = (char*)Dst + count - 1;
Src = (char*)Src + count - 1;
while(count--)
{
*Dst-- = *Src--;
}
}
return pDst;
}
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;
}