删除表达式是否调用析构函数?
我动态地为一个对象分配内存,然后如果我调用删除会发生什么? 调用析构函数或删除函数有不同的处理内存的方式吗?
考虑以下示例:
class A
{
int x;
}
int main()
{
A *ptr = new A();
delete ptr;
return 0;
}
析构函数在哪里调用?
I am allocating memory to a object dynamically and then if I call delete what happens?
the destructor is called or delete function has a different way of handling memory?
Consider the following example:
class A
{
int x;
}
int main()
{
A *ptr = new A();
delete ptr;
return 0;
}
where is the destructor called?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
带有删除运算符的表达式首先调用适当的析构函数(如果需要),然后调用函数运算符delete 来释放存储。
请查看此处了解详细信息。
An expression with the delete operator, first calls the appropriate destructor (if needed), and then calls function operator delete to release the storage.
Have a look here for details.
delete
自动调用析构函数,然后释放内存。delete
automatically calls the destructor, and then frees the memory.是的,
delete
调用析构函数。Yes,
delete
calls the destructor.