码迷,mamicode.com
首页 > 其他好文 > 详细

memcpy、memmove、memset

时间:2015-07-21 12:26:32      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 

memcpy、memmove、memset

标签:

原文地址:http://www.cnblogs.com/jason1990/p/4663820.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!