char *p = new char[10];
p = "asdfasd";
delete p ;
报错:
Debug Assertion Failed!
这是为何?
试了下面的后,自己好好反省一下。
char *p = new char[10];
strcpy(p,"teatsea");
delete [] p;
你现在写出来的东西,编译器都说你错,还和我争,拖下去,砍了~~~~
是应该删除数组。
p = "asdfasd";---->wrong!
"asdfasd" is located in the global memory zone,
you cannt delete the memory from glabal memory zone!
worstly, the bad beheavor like yours will make some memory lost forever,for you allocate char[10],but the statement :p = "asdfasd" makes it hangling
とてもいい問題だろうと思います。
勉強しています。
とてもいい問題だろうと思います。
勉強しています。
_____________
:)
哎, psusong(人类失去指针,世界将会怎样?) 真实高手,一针见血,佩服!
高质量C++/C编程指南
http://www.cnns.net/ref/cppguide.htm#_Toc520633987