memcpy函数实现及其优化

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  num bytes, and should not overlap (for overlapping memory blocks,  memmove  is a safer approach).

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

[cpp]  view plain copy
  1. void *mymemcpy(void *dst,const void *src,size_t num)  
  2. {  
  3.     assert((dst!=NULL)&&(src!=NULL));  
  4.           //assert(des>=src+num||src>dst+num);  
  5.     byte * psrc = (byte *)src;//byte 既为unsigned char类型  
  6.     byte * pdst = (byte *)dst;  
  7.     while(num-->0)*pdst++ = *psrc++;  
  8.     return dst;  
  9. }  


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

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

[cpp]  view plain copy
  1. void * mymemcpy(void *dest, const void *src, size_t count)  
  2. {  
  3.     if (dest == NULL || src == NULL)  
  4.           return NULL;  
  5.     char *pdest = static_cast <char*>(dest);  
  6.     const char *psrc  = static_cast <const char*>(psrc);  
  7.     int n = count;  
  8.       
  9.     if (pdest > psrc && pdest < psrc+count)  
  10.     {  
  11.         for (size_t i=n-1; i != -1; --i)  
  12.         {  
  13.                 pdest[i] = psrc[i];  
  14.         }  
  15.     }  
  16.     else  
  17.     {  
  18.         for (size_t i= 0; i < n; i++)  
  19.         {  
  20.                 pdest[i] = psrc[i];  
  21.         }  
  22.     }  
  23.       
  24.     return dest;  
  25. }  

对memcpy函数的改进:

改进思想:

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

[cpp]  view plain copy
  1. void *mymemcpy(void *dst,const void *src,size_t num)  
  2. {  
  3.     assert((dst!=NULL)&&(src!=NULL));  
  4.     int wordnum = num/4;//计算有多少个32位,按4字节拷贝  
  5.     int slice = num%4;//剩余的按字节拷贝  
  6.     int * pintsrc = (int *)src;  
  7.     int * pintdst = (int *)dst;  
  8.     while(wordnum--)*pintdst++ = *pintsrc++;  
  9.     while (slice--)*((char *)pintdst++) =*((char *)pintsrc++);  
  10.     return dst;  
  11. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值