在CSDN论坛提出了问题:

纠结。。。 这个 char * 该如何创建如何delete的好啊,上网查过资料说是new和delete比较好,
结果自己稍做了一下测试,我纠结了,内存是昨回收的呢? delete为什么会失败了呢?

 

 
  
  1. // Code::Blocks 8.02 +  mingw32-g++.exe  
  2.  
  3. #include <iostream>  
  4. #include <string>  
  5.  
  6. using namespace std;  
  7.  
  8. int main()  
  9. {  
  10. // 初始化变量  
  11.     const char text[] = "const char";               // 长度为10+1  
  12.     char p1 [] = "I'm p1";                          // 长度为6+1  
  13.     char * p2 = new char[100];    p2 = "I'm P2";    // 长度为5或100  
  14.     char * p3 = (char*)malloc(100);   p3 = "I'm P3";// 长度为5或100  
  15.  
  16. // 显示参数  
  17.     cout << "After Construct &&  Before Delete\n";  
  18.     cout << "name" << "\t"  << "Address" << "\t\t" << "Length" << "\t" << "Details" << endl  
  19.          << "text[]" << "\t" << &text    << "\t" << sizeof(text) << "\t" << text  << endl  
  20.          << "p1"     << "\t" << &p1      << "\t" << strlen(p1)   << "\t" << p1    << endl  
  21.          << "p2"     << "\t" << &p2      << "\t" << strlen(p2)   << "\t" << p2    << endl  
  22.          << "p3"     << "\t" << &p3      << "\t" << strlen(p3)   << "\t" << p3    << endl  
  23.          << endl;  
  24.  
  25. // 使用delete或者free删除  
  26.     delete [] text; delete text;             // 纠结,为什么delete个const也不提示错啊  
  27.     delete [] p1;   delete p1;              // 重复delete这么多次也不报错?  
  28.     delete [] p2;   delete p2;  
  29.     delete [] p3;   free(p3);   delete p3;  
  30.  
  31. // 显示参数   昨delete过后也还能继续运行啊?不报错也能正常显示数据?  
  32.     cout << "After Delete\n";  
  33.     cout << "name" << "\t"  << "Address" << "\t\t" << "Length" << "\t" << "Details" << endl  
  34.          << "text[]" << "\t" << &text    << "\t" << sizeof(text) << "\t" << text  << endl  
  35.          << "p1"     << "\t" << &p1      << "\t" << strlen(p1)   << "\t" << p1    << endl  
  36.          << "p2"     << "\t" << &p2      << "\t" << strlen(p2)   << "\t" << p2    << endl  
  37.          << "p3"     << "\t" << &p3      << "\t" << strlen(p3)   << "\t" << p3    << endl  
  38.          << endl;  
  39.  
  40.     return 0;  
  41. }  
  42.  
  43. /**********       结果     ****************  
  44.             ****************  
  45.  
  46. After Construct &&  Before Delete  
  47. name    Address         Length  Details  
  48. text[]  0x28ff20        11      const char  
  49. p1      0x28ff10        6       I'm p1  
  50. p2      0x28ff0c        6       I'm P2  
  51. p3      0x28ff08        6       I'm P3  
  52.  
  53. After Delete  
  54. name    Address         Length  Details  
  55. text[]  0x28ff20        11      const char  
  56. p1      0x28ff10        6       I'm p1  
  57. p2      0x28ff0c        6       I'm P2  
  58. p3      0x28ff08        6       I'm P3  
  59.  
  60.  
  61. Process returned 0 (0x0)   execution time : 0.191 s  
  62. Press any key to continue.  
  63.  
  64.  
  65. *****************************************************/ 



正常编译通过了。。。 

char 数组 该如何新建如何删除是好呢? 求意见~ 
基础问题,大家都来围观啊啊啊~
 

得到大家热心回答~

 

xingfeng2510

const char text[] = "const char";
char p1 [] = "I'm p1";
char * p2 = new char[100]; p2 = "I'm P2";
char * p3 = (char*)malloc(100); p3 = "I'm P3";
短短几行代码存在很多潜在的错误。text、p1均为字符数组,作为临时变量分配在栈上,delete [] text;操作无论是在vc++还是gcc中都会报出类似"warning C4154: 删除数组表达式;转换到提供的指针"的警告,而delete text;在vc++中会出现“ warning C4156: 未使用数组形式的“delete”删除数组表达式;数组形式被替代”的警告,除了数组类型外的其他类型如果显示delete掉栈上的分配的临时变量都会出现运行时错误,因为delete操作只适用于堆内存,这里delete临时数组只是给出了警告而已,其实delete该数组对该数组没有什么影响,原有的数据也不会丢失。因此,lz犯了原则性错误。

 

char * p2 = new char[100]; p2 = "I'm P2";
char * p3 = (char*)malloc(100); p3 = "I'm P3";
delete [] p2; delete p2;
delete [] p3; free(p3); delete p3;
这几行存在内存泄露现象,char * p2 = new char[100];p2指向所分配堆内存块的首地址,而p2 = "I'm P2";这一行会修改p2指针变量自身的值,使其等于静态字符串“I'm P2”的首地址,lz可以调试下,可以观察到p2值的变化,这么delete [] p2; delete p2;还会删除掉原先在堆内存分配的100个字节么? 当然不会。所以终止会导致内存泄露的发生。

 

如何才能用上分配好的内存呢?

逐字节copy, 调用memcpy或strcpy函数即可
  • SKATE11

char * p2 = new char[100]; p2 = "I'm P2"; // 长度为5或100
char * p3 = (char*)malloc(100); p3 = "I'm P3";// 长度为5或100
在=操作符没重载之前不能这样赋值吧楼主 除非你的意愿不是要复制

噢,这仁兄提醒我了,可以运算符重载解决问题。
 

 

总的来说:

char a[] 的时候,创建的char数组会在栈中,无需delete。

char * a 的时候用new, new 了以后要用函数strcpy来赋值,如果直接赋值会改变a的地址。