您的位置:首页 > 编程语言 > C语言/C++

memcpy函数实现及其优化

2015-05-07 21:31 246 查看
1:函数原型void * memcpy ( void * destination, const void * source, size_t num );

函数作用

参考:http://www.cplusplus.com/reference/clibrary/cstring/memcpy/

Copy block of memory
Copies the values of num bytes
from the location pointed by source directly
to the memory block pointed by destination.

The underlying type of the objects pointed by both the source and destination pointers
are irrelevant for this function; The result is a binary copy of the data.

The function does not check for any terminating null character in source -
it always copies exactly num bytes.

To avoid overflows, the size of the arrays pointed by both the destination and source parameters,
shall be at least numbytes,
and should not overlap (for overlapping memory blocks, memmove is
a safer approach).

实现1:《高质量c++,c编程指南》

[cpp] view
plaincopy

void *mymemcpy(void *dst,const void *src,size_t num)  

{  

    assert((dst!=NULL)&&(src!=NULL));  

          //assert(des>=src+num||src>dst+num);  

    byte * psrc = (byte *)src;//byte 既为unsigned char类型  

    byte * pdst = (byte *)dst;  

    while(num-->0)*pdst++ = *psrc++;  

    return dst;  

}  

缺点:没有考虑内存重叠的情况,可以加一个断言换为:assert(des>=src+num||src>dst+num);

实现2:考虑重叠,有重叠情况也复制

[cpp] view
plaincopy

void * mymemcpy(void *dest, const void *src, size_t count)  

{  

    if (dest == NULL || src == NULL)  

          return NULL;  

    char *pdest = static_cast <char*>(dest);  

    const char *psrc  = static_cast <const char*>(psrc);  

    int n = count;  

      

    if (pdest > psrc && pdest < psrc+count)  

    {  

        for (size_t i=n-1; i != -1; --i)  

        {  

                pdest[i] = psrc[i];  

        }  

    }  

    else  

    {  

        for (size_t i= 0; i < n; i++)  

        {  

                pdest[i] = psrc[i];  

        }  

    }  

      

    return dest;  

}  

对memcpy函数的改进:

改进思想:

大部分认为memcpy是一个char到char的拷贝的循环,担心它的效率。实际上,memcpy是一个效率最高的内存拷贝函数,他不会那么傻,来做一个一个字节的内存拷贝,在地址不对齐的情况下,他是一个字节一个字节的拷,地址对齐以后,就会使用CPU字长来拷(和dma类似),32bit或64bit,还会根据cpu的类型来选择一些优化的指令来进行拷贝。总的来说,memcpy的实现是和CPU类型、操作系统、cLib相关的。毫无疑问,它是内存拷贝里效率最高的,请放心使用。

[cpp] view
plaincopy

void *mymemcpy(void *dst,const void *src,size_t num)  

{  

    assert((dst!=NULL)&&(src!=NULL));  

    int wordnum = num/4;//计算有多少个32位,按4字节拷贝  

    int slice = num%4;//剩余的按字节拷贝  

    int * pintsrc = (int *)src;  

    int * pintdst = (int *)dst;  

    while(wordnum--)*pintdst++ = *pintsrc++;  

    while (slice--)*((char *)pintdst++) =*((char *)pintsrc++);  

    return dst;  

}  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ 优化 内存