非正确经验引发C++内存泄露

http://blog.csdn.net/lincyang/article/details/8656456

眼见的事实尚有假,背后的言语未必真。---谚语

当使用另一种方式去实现相同的任务时,过往的经验可以帮助你更快速的分析和实现。但有时候经验也会产生负面影响。

下面的一段例子表示当一个对象以值的形式包含另一个对象时,会自动调用另一个对象的析构函数。

[cpp]  view plain  copy
  1. #include <iostream>  
  2. #include <cstring>  
  3. using namespace std;  
  4. class Tyre  
  5. {  
  6.     char* brand;  
  7. public:  
  8.     Tyre(Tyre &tyre)  
  9.     {  
  10.         cout<<"first construct."<<endl;  
  11.         this->brand = new char[strlen(tyre.brand)+1];  
  12.         strcpy(this->brand,tyre.brand);  
  13.     }  
  14.     Tyre(char* _brand)  
  15.     {  
  16.         cout<<"Tyre("<<_brand<<")"<<endl;  
  17.         this->brand = new char[strlen(_brand)+1];  
  18.         strcpy(this->brand,_brand);  
  19.     }  
  20.     ~Tyre()  
  21.     {  
  22.         cout<<"Tyre destruct:"<<brand<<endl;  
  23.         delete brand;  
  24.     }  
  25.     void print()  
  26.     {  
  27.         cout<<"This is "<<brand<<" Tyre."<<endl;  
  28.     }     
  29. };  
  30.   
  31.   
  32. class Car  
  33. {  
  34.     char* name;  
  35.     Tyre tyre2;  
  36. public:  
  37.     Car(char* name,Tyre tyre):tyre2(tyre)  
  38.     {  
  39.         this->name = new char[strlen(name)+1];  
  40.         strcpy(this->name,name);  
  41.         tyre2.print();  
  42.     }  
  43.     ~Car()  
  44.     {  
  45.         cout<<"Car destructor:only delete name"<<endl;  
  46.         delete name;  
  47.     }  
  48.     void print()  
  49.     {  
  50.         cout<<"Car :";  
  51.         this->tyre2.print();  
  52.     }  
  53. };  
  54.   
  55. int main()  
  56. {  
  57.       
  58.     Tyre tyre1("g");  
  59.     Car theCar("lexus",tyre1);  
  60.     theCar.print();  
  61.       
  62.     return 0;  
  63. }  
编译:gcc -o leak main2.cpp -lstdc++
运行:

[sql]  view plain  copy
  1. D:\workspace\C++\memory_leak\leak2>leak  
  2. Tyre(g)  
  3. first construct.  
  4. first construct.  
  5. This is g Tyre.  
  6. Tyre destruct:g  
  7. Car :This is g Tyre.  
  8. Car destructor:only delete name  
  9. Tyre destruct:g  
  10. Tyre destruct:g  

以引用的方式也一样自动调用其析构:

[cpp]  view plain  copy
  1. //  
  2. #include <iostream>  
  3. #include <cstring>  
  4. using namespace std;  
  5. class Tyre  
  6. {  
  7.     char* brand;  
  8. public:  
  9.     Tyre(char* _brand);  
  10.     ~Tyre();  
  11.     void print();     
  12. };  
  13.   
  14. Tyre::Tyre(char* _brand)  
  15. {  
  16.     cout<<"Tyre("<<_brand<<")"<<endl;  
  17.     this->brand = new char[strlen(_brand)+1];  
  18.     strcpy(this->brand,_brand);  
  19. }  
  20.   
  21. Tyre::~Tyre()  
  22. {  
  23.     cout<<"Tyre destruct:"<<brand<<endl;  
  24.     delete brand;  
  25. }  
  26.   
  27. void Tyre::print()  
  28. {  
  29.     cout<<"This is "<<brand<<" Tyre."<<endl;  
  30. }  
  31.   
  32. class Car  
  33. {  
  34.     char* name;  
  35.     Tyre& tyre2;  
  36. public:  
  37.     Car(char* name,Tyre& tyre);  
  38.     ~Car();  
  39.     void print();  
  40. };  
  41.   
  42. Car::Car(char* name,Tyre& tyre):tyre2(tyre)  
  43. {     
  44.     this->name = new char[strlen(name)+1];  
  45.     strcpy(this->name,name);  
  46.     tyre2.print();  
  47. }  
  48.   
  49. Car::~Car()  
  50. {  
  51.     cout<<"Car destructor:only delete name"<<endl;  
  52.     delete name;  
  53. }  
  54.   
  55. void Car::print()  
  56. {  
  57.     cout<<"Car :";  
  58.     this->tyre2.print();  
  59. }  
  60.   
  61. int main()  
  62. {  
  63.       
  64.     Tyre tyre1("g");  
  65.     Car theCar("lexus",tyre1);  
  66.     theCar.print();  
  67.       
  68.     return 0;  
  69. }  
编译运行结果:

[cpp]  view plain  copy
  1. D:\workspace\C++\memory_leak\leak2>leak  
  2. Tyre(g)  
  3. This is g Tyre.  
  4. Car :This is g Tyre.  
  5. Car destructor:only delete name  
  6. Tyre destruct:g  
而当包含对象的指针时,就不会像之前那样自动调用析构了,必须要像以前的规则,new和delete要成对出现:

[cpp]  view plain  copy
  1. //  
  2. #include <iostream>  
  3. #include <cstring>  
  4. using namespace std;  
  5. class Tyre  
  6. {  
  7.     char* brand;  
  8. public:  
  9.     Tyre(char* _brand);  
  10.     ~Tyre();  
  11.     void print();     
  12. };  
  13.   
  14. Tyre::Tyre(char* _brand)  
  15. {  
  16.     cout<<"Tyre("<<_brand<<")"<<endl;  
  17.     this->brand = new char[strlen(_brand)+1];  
  18.     strcpy(this->brand,_brand);  
  19. }  
  20.   
  21. Tyre::~Tyre()  
  22. {  
  23.     cout<<"Tyre destruct:"<<brand<<endl;  
  24.     delete brand;  
  25. }  
  26.   
  27. void Tyre::print()  
  28. {  
  29.     cout<<"This is "<<brand<<" Tyre."<<endl;  
  30. }  
  31.   
  32. class Car  
  33. {  
  34.     char* name;  
  35.     Tyre* tyre;  
  36. public:  
  37.     Car(char* name,char* _tyre);  
  38.     ~Car();  
  39.     void print();  
  40. };  
  41.   
  42. Car::Car(char* name,char* _tyre)  
  43. {  
  44.     this->name = new char[strlen(name)+1];  
  45.     strcpy(this->name,name);  
  46.     tyre = new Tyre(_tyre);  
  47. }  
  48.   
  49. Car::~Car()  
  50. {  
  51.     cout<<"Car destructor:only delete name"<<endl;  
  52.     delete name;  
  53. //  delete tyre;//Do not forget.  
  54. }  
  55.   
  56. void Car::print()  
  57. {  
  58.     cout<<"Car :";  
  59.     tyre->print();  
  60. }  
  61.   
  62. int main()  
  63. {             
  64.     Car car1("benz","goodyear");  
  65.     car1.print();  
  66.     Car car2("bmw","michelin");  
  67.     car2.print();  
  68.   
  69.     Car *car3 = new Car("audi","continental");  
  70.     car3->print();  
  71.     delete car3;  
  72.       
  73.     return 0;  
  74. }  
运行结果如下:

[sql]  view plain  copy
  1. D:\workspace\C++\memory_leak\leak2>leak  
  2. Tyre(goodyear)  
  3. Car :This is goodyear Tyre.  
  4. Tyre(michelin)  
  5. Car :This is michelin Tyre.  
  6. Tyre(continental)  
  7. Car :This is continental Tyre.  
  8. Car destructor:only delete name  
  9. Car destructor:only delete name  
  10. Car destructor:only delete name  
有三个Tyre没有释放内存,Car::~Car中解开注释,编译运行如下:

[plain]  view plain  copy
  1. D:\workspace\C++\memory_leak\leak2>leak  
  2. Tyre(goodyear)  
  3. Car :This is goodyear Tyre.  
  4. Tyre(michelin)  
  5. Car :This is michelin Tyre.  
  6. Tyre(continental)  
  7. Car :This is continental Tyre.  
  8. Car destructor:only delete name  
  9. Tyre destruct:continental  
  10. Car destructor:only delete name  
  11. Tyre destruct:michelin  
  12. Car destructor:only delete name  
  13. Tyre destruct:goodyear  

生活中不能缺少经验,但不要迷信经验。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值