如果调用析构函数并将分配的内存再次用于其他对象,会发生什么情况? [英] What happens if you call a destructor and use the allocated memory again for other objects?

查看:111
本文介绍了如果调用析构函数并将分配的内存再次用于其他对象,会发生什么情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class base
{
    virtual ~base();
};

class der1 :public base
{
    int i;
}

class der2 :public base //I used double that der2 is bigger than der1
{
    double d;
}

int main()
{
   base* ptr = new der2;
   ptr->~base(); //Destructor call just for the
                 //case that the der2 had a pointer member

   der1* ptr2 = static_cast<der1*>(ptr);
   *ptr2 = der1();

   delete ptr;

   return 0;
}

如果执行上面显示的代码,将会发生什么? 这会导致内存泄漏吗?如果是,为什么呢? 是否有可能在不先释放已占用内存的情况下将其用于其他类型? (请没有答案,例如为什么您需要这样做,这只是出于兴趣)

What would happen if you would execute the code shown above? Would this produce a memory leak and if yes why? Is there a possibility to use the taken memory for different types without releasing it first? (Please no answers like why should you need this, it's just interest)

〜base()不会执行任何操作,因为这是一个示例.

The ~base() does nothing because this is an example.

推荐答案

这是未定义的行为,因为您分配给对象并在对象的生命周期结束后将其删除,这是C ++ 11禁止的,[basic.life]/5:

It's undefined behaviour since you assign to, and delete an object after its lifetime has ended, which is forbidden by C++11, [basic.life]/5:

在对象的生存期结束之后并且在该对象占用的存储空间之前 重用或释放,任何指向对象将要或将要位于的存储位置的指针 只能以有限的方式使用.该程序在以下情况下具有未定义的行为:

after the lifetime of an object has ended and before the storage which the object occupied is reused or released, any pointer that refers to the storage location where the object will be or was located may be used but only in limited ways. The program has undefined behavior if:

  • 对象将是或曾经是具有非平凡析构函数的类类型,并且指针用作a的操作数 delete-expression
  • 指针用于[...]调用对象的非静态成员函数
  • the object will be or was of a class type with a non-trivial destructor and the pointer is used as the operand of a delete-expression
  • the pointer is used to [...] call a non-static member function of the object

(加上一些其他限制.)您尝试同时做这两个事情.

(plus a few other restrictions.) You try to do both of these things.

如果要解决此问题,请在旧内存中创建一个新对象,而不是像在内存中已经存在对象那样分配给它:

If you were to fix this, by creating a new object in the old memory rather than assigning to it as if there were already an object there:

der1* ptr2 = new (ptr) der1;

然后它仍然是不确定的行为,如C ++ 11 [basic.life]/7

then it's still undefined behaviour, as stated in C++11, [basic.life]/7

如果在对象的生存期结束之后并且在该对象占用的存储空间被重用之前,或者 释放后,将在原始对象占用的存储位置创建一个新对象,该指针将 指向原始对象可以 用于操纵新对象,如果新对象与原始对象具有相同的类型

If, after the lifetime of an object has ended and before the storage which the object occupied is reused or released, a new object is created at the storage location which the original object occupied, a pointer that pointed to the original object [...] can be used to manipulate the new object, if [...] the new object is of the same type as the original object

(加上其他一些条件).您可以使用指向旧对象的指针来操纵(特别是删除)其他类型的新对象.

(plus a few other conditions). You use an pointer to the old object to manipulate (specifically, to delete) the new object of a different type.

这篇关于如果调用析构函数并将分配的内存再次用于其他对象,会发生什么情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆