搜尋此網誌

[C/C++][實作memset、memcpy與memcmp]

memset請參考:http://www.cplusplus.com/reference/cstring/memset/
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;
}

memcpy請參考:http://www.cplusplus.com/reference/cstring/memcpy/
void *memcpy(void *dest, const void *src, unsigned long count){

    char *tmp = dest;
    const char *s = src;

    while (count--)
        *tmp++ = *s++;

    return dest;
}

memcmp請參考:http://www.cplusplus.com/reference/cstring/memcmp/
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;
}

沒有留言:

張貼留言