在CSDN论坛提出了问题:
纠结。。。 这个 char * 该如何创建如何delete的好啊,上网查过资料说是new和delete比较好,
结果自己稍做了一下测试,我纠结了,内存是昨回收的呢? delete为什么会失败了呢?
- // Code::Blocks 8.02 + mingw32-g++.exe
- #include <iostream>
- #include <string>
- using namespace std;
- int main()
- {
- // 初始化变量
- const char text[] = "const char"; // 长度为10+1
- char p1 [] = "I'm p1"; // 长度为6+1
- char * p2 = new char[100]; p2 = "I'm P2"; // 长度为5或100
- char * p3 = (char*)malloc(100); p3 = "I'm P3";// 长度为5或100
- // 显示参数
- cout << "After Construct && Before Delete\n";
- cout << "name" << "\t" << "Address" << "\t\t" << "Length" << "\t" << "Details" << endl
- << "text[]" << "\t" << &text << "\t" << sizeof(text) << "\t" << text << endl
- << "p1" << "\t" << &p1 << "\t" << strlen(p1) << "\t" << p1 << endl
- << "p2" << "\t" << &p2 << "\t" << strlen(p2) << "\t" << p2 << endl
- << "p3" << "\t" << &p3 << "\t" << strlen(p3) << "\t" << p3 << endl
- << endl;
- // 使用delete或者free删除
- delete [] text; delete text; // 纠结,为什么delete个const也不提示错啊
- delete [] p1; delete p1; // 重复delete这么多次也不报错?
- delete [] p2; delete p2;
- delete [] p3; free(p3); delete p3;
- // 显示参数 昨delete过后也还能继续运行啊?不报错也能正常显示数据?
- cout << "After Delete\n";
- cout << "name" << "\t" << "Address" << "\t\t" << "Length" << "\t" << "Details" << endl
- << "text[]" << "\t" << &text << "\t" << sizeof(text) << "\t" << text << endl
- << "p1" << "\t" << &p1 << "\t" << strlen(p1) << "\t" << p1 << endl
- << "p2" << "\t" << &p2 << "\t" << strlen(p2) << "\t" << p2 << endl
- << "p3" << "\t" << &p3 << "\t" << strlen(p3) << "\t" << p3 << endl
- << endl;
- return 0;
- }
- /********** 结果 ****************
- ****************
- After Construct && Before Delete
- name Address Length Details
- text[] 0x28ff20 11 const char
- p1 0x28ff10 6 I'm p1
- p2 0x28ff0c 6 I'm P2
- p3 0x28ff08 6 I'm P3
- After Delete
- name Address Length Details
- text[] 0x28ff20 11 const char
- p1 0x28ff10 6 I'm p1
- p2 0x28ff0c 6 I'm P2
- p3 0x28ff08 6 I'm P3
- Process returned 0 (0x0) execution time : 0.191 s
- Press any key to continue.
- *****************************************************/
正常编译通过了。。。
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个字节么? 当然不会。所以终止会导致内存泄露的发生。
如何才能用上分配好的内存呢?
- 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的地址。
转载于:https://blog.51cto.com/neicole/968726