memmove and memcpy
字符串的拷贝函数memmove and memcpy,有什么区别?
来自MSDN上的:
1.memmove:
Copies count bytes (wmemmove) or characters (wmemmove) from src to dest. If some regions of the source area and the destination overlap, both functions ensure that the original source bytes in the overlapping region are copied before being overwritten.
2.memcpy:
memcpy copies count bytes from src to dest; wmemcpy copies count wide characters (two bytes). If the source and destination overlap, the behavior of memcpy is undefined. Use memmove to handle overlapping regions.
当src和 dest有重叠区域时,用memmove可以实现字符串的拷贝,但是另外一个函数memcpy却不可完成。
memmove 的效率低于memcpy。
3.在利用memmove and memcpy进行字符串拷贝时,要注意源字符串的长度,即要把源字符串的'/0'字符拷贝进去。
[/code="c++']
/* memcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1)+1);
memcpy (str3,"copy successful",16);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
输出:
str1: Sample string
str2: Sample string
str3: copy successful
代码修改如下:
[/code="c++']
/* memcpy example */
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[]="Sample string";
char str2[40];
char str3[40];
memcpy (str2,str1,strlen(str1));
memcpy (str3,"copy successful",15);
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
return 0;
}
输出为:
str1: Sample string
str2: Sample string烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘏ample string
str3: copy successful烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫烫蘏ample string烫烫烫烫烫烫
烫烫烫烫烫烫烫烫烫烫烫蘏ample string
原因何在?
memcpy (str2,str1,strlen(str1));这句只是拷贝了Sample string,并没有拷贝字符串的结束符号'\0',才导致此结果。
分享到:
相关推荐
不调用C库函数实现memmove-memcpy函数功能。
。。。
常用函数的实现strcpy,memcpy,memmove,strcat,strcmp原型
那么既然memcpy和memmove二者都是内存拷贝,那二者究竟有什么区别呢? 先说memcpy 你有没有好好的参加过一场C++笔试。让你写出memcpy的实现,这是多么常见的笔试题啊。现在,拿起你的演算纸和笔;是的,是笔和纸,...
今天看到书上降到memcpy和memmove的区别才突然发现原来两者之间有如此区别,以前只知道这两个函数是 实现同样的功能,没有接触到其不同。 memcpy和memmove在MSDN的定义如下: 从两者的声明来看的确没有区别,我们来...
本篇文章是对memmove()与memcpy()的区别以及实现方法进行了详细的分析介绍,需要的朋友参考下
。。。
通过汇编代码实现memmove函数,包括一字节拷贝,四字节拷贝的实现,四字节对齐和非四字节对齐,里面包括各种拷贝情况测试代码
1、讲解内存移动函数memmove的原理,并对比与memcpy函数的区别;
。。。
2.14.5 memmove 2.14.6 memset 2.14.7 strcat 2.14.8 strncat 2.14.9 strchr 2.14.10 strcmp 2.14.11 strncmp 2.14.12 strcoll 2.14.13 strcpy 2.14.14 strncpy 2.14.15 strcspn 2.14.16 strerror 2.14...
1.11 memcpy和memmove函数的实现 1.12 strcat函数实现 1.13 使用库函数atoi,将char *→int 1.14 使用库函数itoa,将int→char * 1.15 不使用库函数,将int→char * 1.16 不使用库函数,将char *→int 1.17 求两个...
数组拷贝,不使用memcpy和memmove,有很多坑,避免内存溢出
memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...
memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...
memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...
memcpy memicmp memmove memset movmem setmem stpcpy strcat strchr strcmp strcmpi strcpy strcspn strdup stricmp strlen strlwr strncat strncmp strncmpi strncpy strnicmp strpbrk ...