库函数memcpy()与memmove()实现

根据MSDN文档,当源区域与目标区域存在重叠时,memcpy()函数报错,而memmove()函数可以处理重叠情况!
 1 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 /* 
 2库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 * 函数名: memcpy 
 3库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 * 功  能: 从源source中拷贝n个字节到目标destin中 
 4库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 * 用  法: void *memcpy(void* destin, const void* source, size_t n); 
 5库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 * 说  明: 内存拷贝
 6库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客*/

 7 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
 8 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客#include  < stdio.h >  
 9 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客#include  < conio.h >     // getch头文件
10 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 #include  < assert.h >    // assert头文件
11 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
12 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客typedef unsigned  char   byte
13 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 // typedef unsigned int size_t;
14 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
15 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
16 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 /*
17库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客memcpy函数,如果内存重叠则报错
18库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客*/

19 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 // src要保留
20 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 void *  memcpy( void *  dst, const   void *  src,size_t count) 
21 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 {
22库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    byte* pbTo = (byte*)dst; 
23库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    byte* pbFrom = (byte*)src; 
24库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    assert(dst!= NULL && src != NULL);//不能存在空指针
25库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    assert(pbTo >= pbFrom+count || pbFrom >= pbTo + count);//防止内存重叠(overlap) 
26库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    while (count-- > 0
27库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    
28库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        *pbTo++ = *pbFrom++
29库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    }
 
30库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    return dst; 
31库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客}

32 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
33 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 /*
34库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客memmove函数,考虑了内存重叠的情况
35库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客*/

36 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 // src可以不保留
37 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 void *  memmove( void *  dst, const   void *  src,size_t count) 
38 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客 {     
39库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    byte* pbTo = (byte*)dst; 
40库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    byte* pbFrom = (byte*)src; 
41库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    assert(dst != NULL && src != NULL);//不能存在空指针
42库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    if (dst <= src || pbTo >= pbFrom + count)// 
43库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    
44库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        while (count-- > 0
45库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        
46库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客            *pbTo++ = *pbFrom++; //按递增拷贝
47库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        }
 
48库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    }
 
49库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    else  //
50库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    
51库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        pbTo = pbTo + count -1;//overlap的情况,从高位地址向低位拷贝 
52库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        pbFrom = pbFrom + count -1
53库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        while (count-- > 0
54库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        
55库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客            *pbTo-- = *pbFrom--; //按递减拷贝
56库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客        }
 
57库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    }
 
58库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客    return dst; 
59库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客}

60 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
61 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客
62 库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值