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

本文深入探讨了C语言中的memcpy和memmove函数的区别,特别是当源区域与目标区域存在重叠时,memmove如何避免错误并有效进行内存复制。通过对比分析,了解如何在不同场景下选择合适的函数进行高效且安全的数据迁移。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据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的博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值