读书人

char * 的 delete 有关问题看见运行

发布时间: 2012-09-19 13:43:53 作者: rapoo

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

C/C++ code
// 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 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;    return 0;}/**********       结果     ****************            ****************After Construct &&  Before Deletename    Address         Length  Detailstext[]  0x28ff20        11      const charp1      0x28ff10        6       I'm p1p2      0x28ff0c        6       I'm P2p3      0x28ff08        6       I'm P3After Construct &&  Before Deletename    Address         Length  Detailstext[]  0x28ff20        11      const charp1      0x28ff10        6       I'm p1p2      0x28ff0c        6       I'm P2p3      0x28ff08        6       I'm P3Process returned 0 (0x0)   execution time : 0.140 sPress any key to continue.*****************************************************/


正常编译通过了。。。

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


[解决办法]
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个字节么? 当然不会。所以终止会导致内存泄露的发生。
[解决办法]
正常编译通过了?虽然可以编译通过,但一路warning,且会出现运行时错误,无论是vc编译器还是gcc编译器都不会让你顺利的运行,因为delete栈上分配的变量是c++编程中的大忌,一定要切记。
[解决办法]
逐字节copy, 调用memcpy或strcpy函数即可
[解决办法]
刚才试了下,gcc与vc编译器均可以编译通过,但运行时出现运行时错误且均因内存地址不可读或堆栈被破坏而终止运行。不知为何lz的Code Blocks下的gcc可以运行?
[解决办法]
char * p2 = new char[100]; p2 = "I'm P2"; // 长度为5或100
char * p3 = (char*)malloc(100); p3 = "I'm P3";// 长度为5或100
在=操作符没重载之前不能这样赋值吧楼主 除非你的意愿不是要复制

读书人网 >C++

热点推荐