memcpy、memmove、memset

void *  memcpy(void * dst, const void * src, size_t count)
{
    void *res=dst;

    while (count--)
    {
        *(char*)dst = *(char*)src;
        dst = (char*)dst + 1;
        src = (char*)src + 1;
    }

    return res;
}
void *  memmove(void * dst, const void * src, size_t count)
{
    void *res = dst;

    //重叠部分不影响或者不存在重叠
    if (dst <= src || (char*)src + count <= (char*)dst)
    {
        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 res;
}
void* memset(void* dst, int val, size_t count)
{
    void* res = dst;

    while (count--)
    {
        *(char*)dst = (char)val;
        dst = (char*dst) + 1;
    }

    return res;
}

 

posted @ 2015-07-21 11:06  从此寂静无声  阅读(244)  评论(0编辑  收藏  举报