搜尋此網誌

2013年1月17日 星期四

memset/memcpy/memcmp --- 簡單的實作


void *memset(void *s, char c, unsigned long n)
{
unsigned long  i;
char *ss = (char *)s;

for (i = 0; i < n; i++) ss[i] = (char)c;
return s;
}

void *memcpy(void *dest, const void *src, unsigned long count)
{
char *tmp = dest;
const char *s = src;

while (count--)
*tmp++ = *s++;
return dest;
}

int memcmp(const void *cs, const void *ct, unsigned long count)
{
const unsigned char *su1, *su2;
int res = 0;

for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != 0)
break;
return res;
}

沒有留言:

張貼留言