memcpy 的内存拷贝函数

 1 #include <iostream>
 2 
 3 using namespace std;
 4 
 5 void *memory(void *dst,const void *src,size_t s)
 6 {
 7 
 8    const char* psrc=static_cast<const char*>(src);
 9     char* pdst=static_cast<char*>(dst);
10 
11     if(psrc==NULL||pdst==NULL)
12         return NULL;
13 
14     if(pdst>psrc&&pdst<(psrc+s))
15     {
16         for(size_t i=s-1;i!=-1;i--)
17             pdst[i]=psrc[i];
18     }
19     else
20     {
21         for(size_t i=0;i<s;++i)
22             pdst[i]=psrc[i];
23     }
24     return dst;
25 }
26 int main()
27 {
28    char buf[100]="abcdefghijk";
29    cout << buf << endl;
30    memory(buf+2,buf,5);
31    cout << buf;
32    return 0;
33 }

 函数memcpy是实现,主要是对函数memcpy的理解透彻,防止内存自己拷贝自己,存在从前向后拷贝和从后向前拷贝的顺序。

转载于:https://www.cnblogs.com/mingyoujizao/p/9525130.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值