"delete this;" in the member function

本文探讨了一道经典的C++面试题:是否可以在成员函数中调用“delete this”。通过实践测试和理论分析,揭示了不同情景下(堆栈与堆内存)调用此语句的行为差异及其原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

在网上看到了一个NB的文章:最好的C++面试题目。

http://blogs.windwardreports.com/davidt/2009/12/the-best-c-interview-question-ever.html

 

原文:

There is no one magical question that can determine if someone is the right candidate. There isn't even one that will determine if their technical knowledge is up to par. But there are those occasional questions that provide a lot of bang for the buck – simple questions that give you a lot of feedback about the candidate.

If I could ask a C++ programmer just one technical question, this would be it. Why? Because the correct answer feels wrong and the initial answer is generally wrong. So seeing how they work through the explanation is very illustrative of not just their knowledge of C++, but their willingness to not get locked in to their initial answer.

Q: Can you call "delete this;" inside a member function?

The instinctual answer of almost everyone is No. Not only no but there is a visceral emotional response from most that doing so would be like crossing the beams in Ghostbusters – very bad news. Ok, so next step, regardless of if they answer Yes or No is to ask "Why?"

This is where you really learn a lot. Even people who answer Yes (and they are few and far between), most of them have trouble explaining what is going on. I have seen many, over 5 – 10 very painful minutes, sketch out most of what is going on, and yet still say that you cannot do it. These are people you do not want to hire under any circumstances because they cannot think outside their initial preconceptions.

I have also had many with some of the most outlandish understandings of what is actually going on in the computer. Many people think when you call the delete that the memory the code is running on is garbage collected. Not the data – the code! Some think the v-table disappears on calling a delete (it doesn't as it's tied to the class) – and they think the v-table is needed for the return from the method.

Ok, so you then have one of the few who has worked through the above, got it figured out, and you're feeling good. We then have a follow-up question that will drop out half of the remaining candidates – "What can you do after calling delete this?"

And you will get answers that are all over the board from "anything" to "nothing." I've had many ask me if local variables are consider part of the object memory – so at least those people are thinking it through even if they don't fully understand what belongs to an object.

So there you go – a great interview question until it is over-used enough that everyone knows about it (like the manhole cover question).

Update: This has received a large number of comments (especially on reddit). I want to stress one key point - I don't ask this because I expect most people to get the initial answer right. If they did it would not be that useful a question. I ask to see if they can work their way to the correct answer and how they do so. As to the question of why would you ever do this - it is very useful in message based systems and when threads are exiting.

Ps – If you have a great "just one question" – please post it in a comment below.

 

 

针对best,褒贬不一。我特意写了一个程序来做测试:

执行结果:

ewuming/tmp> ./c.out
Dog constructor
----------->Dog2<------------------
Dog::print()
Dog::release()
Dog deconstructor
Dog constructor
----------->Dog1<------------------
Dog::print()
Dog::release()
Dog deconstructor
Segmentation fault

 

分析:

通常情况下,类的内存可能出现在两个地方:stack和heap。

stack中,保存的是整个class的所有信息,在这个function执行return的时候,执行析构函数。

heap中,class的所有信息保存在heap中,只有在delete的时候才执行析构函数。

 

根据执行结果,heap中的class,member function可以有delete this;stack中的class,member function中如果有delete this,则出现了segementation fault。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值