目录
前言
* new申请的内存,即使你没有执行delete,在程序结束后,也会释放掉的。
* 但这样做会有很多弊端,下文将列举部分情况。
弊端
不delete的话被释放时不会执行析构函数
- 测试 - 在vs2022中:
- 定义一个test类,并在main函数中声明t1,t2,t3三个对象:
#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;
}
- 可以看到在main函数中,t2,t3是new出来的,我们手动把 t2 delete。
- 运行结果:
![](https://blog.coolight.cool/wp-content/uploads/2022/03/image-24.png)
- 可以看到,t2因为我们手动delete而在main结束前执行了析构函数
- t1则main结束后执行了析构函数。
- t3则是由系统回收,可见它并没有执行析构函数
- 因此,如果析构函数中有输出提示,缓冲内容等需要执行,则应该手动delete。当然,即便没有也倡导自己手动delete。
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.
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!
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!
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.
I enjoy reading a post that can make men and women think. Also, thanks for allowing for me to comment.
Great article. I am dealing with some of these issues as well..
You should take part in a contest for one of the most useful websites online. I’m going to highly recommend this blog!
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.
Great site you have here.. It’s hard to find quality writing like yours these days. I truly appreciate individuals like you! Take care!!
I used to be able to find good advice from your articles.
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.
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.
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.
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.
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.
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.
amoxicillin 500 mg purchase without prescription: amoxicillin 500mg price canada – amoxicillin 500 mg without prescription