"delete this;" in the member function
2010-03-03 13:04
405 查看
在网上看到了一个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。
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,褒贬不一。我特意写了一个程序来做测试:
#include <iostream> using namespace std; class Dog { public: Dog() { std::cout << "Dog constructor" << endl; } ~Dog() { std::cout << "Dog deconstructor" << endl; } void release() { std::cout << "Dog::release()" << endl; delete this; } void print() { std::cout << "Dog::print()" << endl; } private: int m_i; }; int main() { Dog *pDog2 = new Dog(); std::cout << "----------->Dog2<------------------" << endl; pDog2->print(); pDog2->release(); Dog dog1 = Dog(); std::cout << "----------->Dog1<------------------" << endl; dog1.print(); dog1.release(); return 1; }
执行结果:
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。
相关文章推荐
- "this whole ARM thing is a f*cking pain in the ass"
- How to fix Rosetta stone error 2123 -- "This was an error in the application"
- "The Civil War in France" translation04
- Error:'Please set up the forwarding function parameter "wipnp" in the workstation startup function!'
- The project was not built due to "Could not delete ''.". Fix the problem, then try refreshing this p
- About Derby "ERROR 42X51: The class '***' does not exist or is inaccessible. This can hap pen if the class is not public."
- "Cannot declare member function ...to have static linkage"错误
- django提交表单提示"You called this URL via POST, but the URL doesn't end in a slash and you have APPEND_SL
- "An extender can't be in a different UpdatePanel than the control it extends." error really needs debugging information ,错误
- One of the disks in this virtual machine is already in use by a virtual machine or by a snapshot"
- C++中的"delete this"
- "The length of the string value exceeds the length configured in the mapping/parameter."}
- "The string '__checksum' was not found in the string table"解决办法
- "this class is not key value coding-compliant for the key ..."问题的解决(转)
- "The object cannot be deleted because it was not found in the ObjectStateManager."
- 装XP系统时蓝屏,出现“The BIOS in this system is not fully ACPI compliant.."
- 使用用sudo时提示"### is not in the sudoers file. This incident will be reported.
- ADODB使用excel的"Cannot modify the design of table 'xxxx'. It is in a read-only database"的问题
- "Cannot declare member function ...to have static linkage"错误
- "this class is not key value coding-compliant for the key ..."问题的解决