memmove、memcpy和memccpy三个函数都是内存的拷贝,从一个缓冲区拷贝到另一个缓冲区。表头文件: #include <string.h>
memcpy
memcpy(void *dest,void *src,int count)
定义函数: void *memcpy(void *dest, const void *src, size_t n)
函数说明: memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'\0'而结束
返回值: 返回指向dest的指针
memccpy
memccpy(void*dest,void*src,int ch,int count)
定义函数: void *memccpy(void *dest, const void *src, int c, size_t n);
函数说明: memccpy()用来拷贝src所指的内存内容前n个字节到dest所指的地址上。与memcpy()不同的是,memccpy()如果在src中遇到某个特定值(int c)立即停止复制。
返回值: 返回指向dest中值为c的下一个字节指针。返回值为0表示在src所指内存前n个字节中没有值为c的字节。
memmove
memmove(void *dest,void*src,int count)
定义函数: void *memmove(void *dest, const void *src, size_t n);函数说明:memmove()是从一个缓冲区 复制 到另一个缓冲区中。 返回值: 返回指向dest指针。请看文档描述
Name: memmove
Prototype: void * memmove (void *to, const void *from, size_t size)
Description:
memmove copies the size bytes at from into the size bytes at to, even if those two blocks of space overlap. In the
case of overlap, memmove is careful to copy the original values of the bytes in the block at from, including those
bytes which also belong to the block at to.
The value returned by memmove is the value of to.
Header files:
string.h
当dest <= src-count 或dest >= src+count时,以上三个函数均不会产生覆盖问题,即源数据不会被更改。
若不在以上范围内,则源数据会被更改。
代码测试
/*
============================================================================
Name : t2.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
//puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
char a[]={'a','b'};
char b[]={'e','f','c','d'};
//memmove(a,b,sizeof(b));
memmove(a,b,sizeof(b));
printf(" b[0]is %c\n",b[0]);
printf(" b[1]is %c\n",b[1]);
printf(" b[2]is %c\n",b[2]);
printf(" b[3]is %c\n",b[3]);
return EXIT_SUCCESS;
}
结果
b[0]is c
b[1]is d
b[2]is c
b[3]is d
将函数还为memcpy,memccpy结果一样。
接下来我们换个实验
/*
============================================================================
Name : t2.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
//puts("!!!Hello World!!!"); /* prints !!!Hello World!!! */
char a[]={'a','b'};
char b[]={'e','f','c','d'};
char *c = b+2;
//memmove(a,b,sizeof(b));
memcpy(c,b,sizeof(b));
printf(" b[0]is %c\n",b[0]);
printf(" b[1]is %c\n",b[1]);
printf(" b[2]is %c\n",b[2]);
printf(" b[3]is %c\n",b[3]);
return EXIT_SUCCESS;
}
结果
*** stack smashing detected ***: /home/wyx/workspace/t2/Debug/t2 terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb76dc0e5]
/lib/i386-linux-gnu/libc.so.6(+0x10409a)[0xb76dc09a]
/home/wyx/workspace/t2/Debug/t2[0x80484f9]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75f14d3]
/home/wyx/workspace/t2/Debug/t2[0x80483a1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:09 602814 /home/wyx/workspace/t2/Debug/t2
08049000-0804a000 r--p 00000000 08:09 602814 /home/wyx/workspace/t2/Debug/t2
0804a000-0804b000 rw-p 00001000 08:09 602814 /home/wyx/workspace/t2/Debug/t2
08b35000-08b56000 rw-p 00000000 00:00 0 [heap]
b75a8000-b75c4000 r-xp 00000000 08:08 33488 /lib/i386-linux-gnu/libgcc_s.so.1
b75c4000-b75c5000 r--p 0001b000 08:08 33488 /lib/i386-linux-gnu/libgcc_s.so.1
b75c5000-b75c6000 rw-p 0001c000 08:08 33488 /lib/i386-linux-gnu/libgcc_s.so.1
b75d7000-b75d8000 rw-p 00000000 00:00 0
b75d8000-b777b000 r-xp 00000000 08:08 33467 /lib/i386-linux-gnu/libc-2.15.so
b777b000-b777d000 r--p 001a3000 08:08 33467 /lib/i386-linux-gnu/libc-2.15.so
b777d000-b777e000 rw-p 001a5000 08:08 33467 /lib/i386-linux-gnu/libc-2.15.so
b777e000-b7781000 rw-p 00000000 00:00 0
b7790000-b7794000 rw-p 00000000 00:00 0
b7794000-b7795000 r-xp 00000000 00:00 0 [vdso]
b7795000-b77b5000 r-xp 00000000 08:08 33447 /lib/i386-linux-gnu/ld-2.15.so
b77b5000-b77b6000 r--p 0001f000 08:08 33447 /lib/i386-linux-gnu/ld-2.15.so
b77b6000-b77b7000 rw-p 00020000 08:08 33447 /lib/i386-linux-gnu/ld-2.15.so
bfc64000-bfc85000 rw-p 00000000 00:00 0 [stack]
b[0]is e
b[1]is f
b[2]is e
b[3]is f
换为memmove结果
b[0]is e
b[1]is f
b[2]is e
b[3]is f
*** stack smashing detected ***: /home/wyx/workspace/t2/Debug/t2 terminated
======= Backtrace: =========
/lib/i386-linux-gnu/libc.so.6(__fortify_fail+0x45)[0xb76520e5]
/lib/i386-linux-gnu/libc.so.6(+0x10409a)[0xb765209a]
/home/wyx/workspace/t2/Debug/t2[0x8048539]
/lib/i386-linux-gnu/libc.so.6(__libc_start_main+0xf3)[0xb75674d3]
/home/wyx/workspace/t2/Debug/t2[0x80483d1]
======= Memory map: ========
08048000-08049000 r-xp 00000000 08:09 602814 /home/wyx/workspace/t2/Debug/t2
08049000-0804a000 r--p 00000000 08:09 602814 /home/wyx/workspace/t2/Debug/t2
0804a000-0804b000 rw-p 00001000 08:09 602814 /home/wyx/workspace/t2/Debug/t2
0908c000-090ad000 rw-p 00000000 00:00 0 [heap]
b751e000-b753a000 r-xp 00000000 08:08 33488 /lib/i386-linux-gnu/libgcc_s.so.1
b753a000-b753b000 r--p 0001b000 08:08 33488 /lib/i386-linux-gnu/libgcc_s.so.1
b753b000-b753c000 rw-p 0001c000 08:08 33488 /lib/i386-linux-gnu/libgcc_s.so.1
b754d000-b754e000 rw-p 00000000 00:00 0
b754e000-b76f1000 r-xp 00000000 08:08 33467 /lib/i386-linux-gnu/libc-2.15.so
b76f1000-b76f3000 r--p 001a3000 08:08 33467 /lib/i386-linux-gnu/libc-2.15.so
b76f3000-b76f4000 rw-p 001a5000 08:08 33467 /lib/i386-linux-gnu/libc-2.15.so
b76f4000-b76f7000 rw-p 00000000 00:00 0
b7706000-b770a000 rw-p 00000000 00:00 0
b770a000-b770b000 r-xp 00000000 00:00 0 [vdso]
b770b000-b772b000 r-xp 00000000 08:08 33447 /lib/i386-linux-gnu/ld-2.15.so
b772b000-b772c000 r--p 0001f000 08:08 33447 /lib/i386-linux-gnu/ld-2.15.so
b772c000-b772d000 rw-p 00020000 08:08 33447 /lib/i386-linux-gnu/ld-2.15.so
bfd3a000-bfd5b000 rw-p 00000000 00:00 0 [stack]