根据MSDN文档,当源区域与目标区域存在重叠时,memcpy()函数报错,而memmove()函数可以处理重叠情况!
1
/*
2
* 函数名: memcpy
3
* 功 能: 从源source中拷贝n个字节到目标destin中
4
* 用 法: void *memcpy(void* destin, const void* source, size_t n);
5
* 说 明: 内存拷贝
6
*/
7![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
8
#include
<
stdio.h
>
9
#include
<
conio.h
>
//
getch头文件
10
#include
<
assert.h
>
//
assert头文件
11
12
typedef unsigned
char
byte
;
13
//
typedef unsigned int size_t;
14
15![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
16
/*
17
memcpy函数,如果内存重叠则报错
18
*/
19
//
src要保留
20
void
*
memcpy(
void
*
dst,
const
void
*
src,size_t count)
21
{
22
byte* pbTo = (byte*)dst;
23
byte* pbFrom = (byte*)src;
24
assert(dst!= NULL && src != NULL);//不能存在空指针
25
assert(pbTo >= pbFrom+count || pbFrom >= pbTo + count);//防止内存重叠(overlap)
26
while (count-- > 0)
27
{
28
*pbTo++ = *pbFrom++;
29
}
30
return dst;
31
}
32![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
33
/*
34
memmove函数,考虑了内存重叠的情况
35
*/
36
//
src可以不保留
37
void
*
memmove(
void
*
dst,
const
void
*
src,size_t count)
38
{
39
byte* pbTo = (byte*)dst;
40
byte* pbFrom = (byte*)src;
41
assert(dst != NULL && src != NULL);//不能存在空指针
42
if (dst <= src || pbTo >= pbFrom + count)//
43
{
44
while (count-- > 0)
45
{
46
*pbTo++ = *pbFrom++; //按递增拷贝
47
}
48
}
49
else //
50
{
51
pbTo = pbTo + count -1;//overlap的情况,从高位地址向低位拷贝
52
pbFrom = pbFrom + count -1;
53
while (count-- > 0)
54
{
55
*pbTo-- = *pbFrom--; //按递减拷贝
56
}
57
}
58
return dst;
59
}
60![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
61![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
62
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
2
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
3
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
4
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
5
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
6
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
7
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
8
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
9
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
10
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
11
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
12
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
13
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
14
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
15
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
16
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
17
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
18
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
19
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
20
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
21
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
22
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
23
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
24
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
25
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
26
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
27
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
28
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
29
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
30
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
31
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
32
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
33
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
34
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
35
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
36
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
37
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
38
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/2f88ce130b654eb5dc6788e02dbcfc90.gif)
39
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
40
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
41
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
42
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
43
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
44
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
45
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
46
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
47
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
48
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
49
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
50
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
51
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
52
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
53
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
54
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/f70a0fde2b51b7dd92a70e712e540cf6.gif)
55
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
56
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
57
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/4a5daaec04350a363f186a4d2c5ed6ce.gif)
58
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/df37983f39daa189b8c814e01a6a9011.gif)
59
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/0ac3a2d53663ec01c7f7225264eeefae.gif)
60
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
61
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)
62
![库函数memcpy()与memmove()实现 - 雨印 - Thinker的博客](https://i-blog.csdnimg.cn/blog_migrate/cbef093dcc044b2793832001e2365e43.gif)