memcpy和strcpy的区别是什么?

shaxiren 2003-08-22 08:39:55
尤其是memcpy的用法,哪位大侠指教一下用法
...全文
212 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
mgh001 2003-08-22
  • 打赏
  • 举报
回复
编译器不是万能的呀! :-<
mgh001 2003-08-22
  • 打赏
  • 举报
回复
Meuck(暗流,如果你相信什么,你就应当有所了解) 说的对!
"好运的话,就会有非法操作罗.
边界问题要程序员自己解决"
我也试了以下,编译通过,运行时"非法操作"--VC++6.0环境
shaxiren 2003-08-22
  • 打赏
  • 举报
回复
无论memcpy还是strcpy,一旦dest长度不够,按道理应该报错才对啊
DDrddr 2003-08-22
  • 打赏
  • 举报
回复
“即使strdest的长度比strsrc小很多,也照样能把strsrc的内容全部考过来”
这是可能的,这就是c/c++的特点。拷贝的时候长度是以strscr为准的,当拷贝没结束的时候(即:在strscr中没找到‘\0’时)一直复制。如下:
while( ( *strdest++ = *strsrc++ ) != '\0' );
你的程序之所以正常输出,是因为*src的"hello world",在拷贝到dest后没有影响到程序的其他部分,或者影响的很少。程序可以继续运行。

memcpy是完全听你的指挥的,叫它考多少就考多少。
Meuck 2003-08-22
  • 打赏
  • 举报
回复
好运的话,就会有非法操作罗.
边界问题要程序员自己解决。
shaxiren 2003-08-22
  • 打赏
  • 举报
回复
void *memcpy(void *destin, void *source, unsigned n);
如果,source的前n个字节的长度已经超过了destin,有当如何
shaxiren 2003-08-22
  • 打赏
  • 举报
回复
我发现一个问题不知道大家注意到没有:
char *strcpy(char *strDest, const char *strSrc);
即使strdest的长度比strsrc小很多,也照样能把strsrc的内容全部考过来!!
如char dest[1];
char *src="hello world";
strcpy(dest,src);
cout<<dest;
输出竟然是"hello world",按理来说dest的空间不够应该报错啊!
njuhuangmy 2003-08-22
  • 打赏
  • 举报
回复
需要 注意的 就是 如果 src+size > dst 且 src < dst 的时候

strcpy copy 的结果 和 memcpy的 结果可能不一样
kenryHuang 2003-08-22
  • 打赏
  • 举报
回复
memcpy(dst, src, size);
从src处拷贝size个bytes到dst处
strcpy(dst, src);
从src处拷贝字符到dst处,一直到'\0'
shishiXP 2003-08-22
  • 打赏
  • 举报
回复
一个拷贝内存,什么都能拷

一个拷贝字符串
yzb1000 2003-08-22
  • 打赏
  • 举报
回复
看看msdn的解释就知道了
The strcpy function copies strSource, including the terminating null character, to the location specified by strDestination. No overflow checking(这里) is performed when strings are copied or appended. The behavior of strcpy is undefined if the source and destination strings overlap.
再看memcpy
The memcpy function copies count bytes of src to dest. If the source and destination overlap, this function does not ensure that the original source bytes in the overlapping region are copied before being overwritten. Use memmove to handle overlapping regions。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧