[c++]new之后不delete的弊端

/ 3,367评论 / 11218阅读 / 0点赞

前言

* new申请的内存,即使你没有执行delete,在程序结束后,也会释放掉的。

* 但这样做会有很多弊端,下文将列举部分情况。


弊端

不delete的话被释放时不会执行析构函数

#include<iostream>
using namespace std;

class test
{
	string name;
public:
	test(const string& in_name)
	{
		name = in_name;
		cout << "<< " + name + " 出现了,指针:" << this << endl;
	}


	~test()
	{
		cout << "<< " + name + "执行了析构函数,指针:" << this << endl;
	}

};

int main()
{
	test t1("t1");

	test* t2 = new test("t2");

	test* t3 = new test("t3");

	cout << endl << endl;

	delete t2;

	cout << endl << endl << "<< main - return" << endl << endl;

	return 0;
}

可能导致运行时占用大量内存,影响程序长时间运行

  1. Mp3Juices说道:

    The next time I read a blog, I hope that it does not disappoint me just as much as this one. I mean, Yes, it was my choice to read, nonetheless I genuinely believed you would probably have something interesting to talk about. All I hear is a bunch of complaining about something you could possibly fix if you were not too busy seeking attention.

  2. You need to be a part of a contest for one of the most useful sites on the web. I will highly recommend this website!

  3. kontol besar说道:

    Hi there! This post could not be written any better! Looking through this article reminds me of my previous roommate! He continually kept talking about this. I’ll send this post to him. Pretty sure he’s going to have a great read. I appreciate you for sharing!

  4. ngentot memek说道:

    After going over a number of the articles on your web page, I seriously like your way of blogging. I saved as a favorite it to my bookmark site list and will be checking back in the near future. Please visit my website as well and tell me your opinion.

  5. I enjoy reading a post that can make men and women think. Also, thanks for allowing for me to comment.

  6. reference说道:

    Great article. I am dealing with some of these issues as well..

  7. Galileo FX说道:

    You should take part in a contest for one of the most useful websites online. I’m going to highly recommend this blog!

  8. Michael说道:

    You’ve made some decent points there. I looked on the internet for more info about the issue and found most people will go along with your views on this web site.

  9. tubidy mp3说道:

    Great site you have here.. It’s hard to find quality writing like yours these days. I truly appreciate individuals like you! Take care!!

  10. IWIN说道:

    I used to be able to find good advice from your articles.

  11. Thomas Tom Dwan说道:

    I was excited to uncover this web site. I need to to thank you for ones time for this wonderful read!! I definitely appreciated every part of it and i also have you bookmarked to see new information on your website.

  12. ytmp3说道:

    May I simply say what a comfort to uncover somebody who actually knows what they’re discussing on the net. You definitely realize how to bring a problem to light and make it important. More and more people really need to read this and understand this side of your story. I was surprised that you are not more popular given that you most certainly have the gift.

  13. jacket company说道:

    That is a very good tip particularly to those new to the blogosphere. Brief but very precise information… Many thanks for sharing this one. A must read post.

  14. tubidy mp3说道:

    You have made some decent points there. I looked on the web to find out more about the issue and found most individuals will go along with your views on this site.

  15. I’m very pleased to discover this website. I want to to thank you for your time just for this fantastic read!! I definitely liked every little bit of it and I have you saved as a favorite to check out new information in your site.

  16. I absolutely love your blog.. Pleasant colors & theme. Did you make this amazing site yourself? Please reply back as I’m looking to create my own blog and want to learn where you got this from or exactly what the theme is named. Kudos.

  17. Jamesjer说道:

    amoxicillin 500 mg purchase without prescription: amoxicillin 500mg price canada – amoxicillin 500 mg without prescription

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注