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

来源:互联网 发布:弹指神功打字软件 编辑:程序博客网 时间:2024/05/21 08:43
根据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的博客