点击(此处)折叠或打开
-
char str[]=\"123456789\";
-
char dst[]=\"aaa\";
-
strcpy(dst, str);
-
cout<<str<<endl;
-
cout<<dst<<endl;
-
cout<<&str<<endl;
- cout<<&dst<<endl;
- 因为dst和str内存相连 所以str复制到dst时 把自己给覆盖了

点击(此处)折叠或打开
-
char str[10]=\"123456789\";
-
char dst[10]=\"aaa\";
-
strcpy(dst, str);
-
cout<<str<<endl;
-
cout<<dst<<endl;
-
cout<<&str<<endl;
- cout<<&dst<<endl;

点击(此处)折叠或打开
-
char dst[]=\"aaa\";
-
char str[]=\"123456789\";
-
char *p13=(char *)0x0012FF44;
-
char *p12=(char *)0x0012FF43;
-
char *p11=(char *)0x0012FF42;
-
char *p10=(char *)0x0012FF41;
-
char *p9=(char *)0x0012FF40;
-
char *p=(char *)0x0012FF3F;
-
char *p1=(char *)0x0012FF3F;
-
char *p2=(char *)0x0012FF3E;
-
char *p3=(char *)0x0012FF3D;
-
char *p4=(char *)0x0012FF3C;
-
char *p5=(char *)0x0012FF3B;
-
char *p6=(char *)0x0012FF3A;
-
char *p7=(char *)0x0012FF39;
-
char *p8=(char *)0x0012FF38;
-
cout<<*p13<<endl;
-
cout<<*p12<<endl;
-
cout<<*p11<<endl;
-
cout<<*p10<<endl;
-
cout<<*p9<<endl;
-
cout<<*p<<endl;
-
cout<<*p1<<endl;
-
cout<<*p2<<endl;
-
cout<<*p3<<endl;
-
cout<<*p4<<endl;
-
cout<<*p5<<endl;
-
cout<<*p6<<endl;
-
cout<<*p7<<endl;
-
cout<<*p8<<endl;
-
cout<<&dst<<endl;
- cout<<&str<<endl;
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/29012686/viewspace-1141720/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/29012686/viewspace-1141720/
本文探讨了C++中字符串复制过程中的一个常见问题:当源字符串与目标字符串在内存中相邻时,使用strcpy函数可能导致的数据覆盖现象。通过具体代码示例展示了如何复现这一问题,并提供了避免该问题的方法。
2724

被折叠的 条评论
为什么被折叠?



