English 中文(简体)
整个物体是否与一个非虚拟的脱轨器和基地级点子释放?
原标题:Is the whole object freed with a non-virtual destructor and a Base class pointer?

If a Base class does not have a virtual destructor (in order to avoid the vtable entry for instance) and the Derived class has only basic attributes, does it free all the memory allocated by new, when the pointer of the Base class is deleted? I know the destructor of the Derived class will not be called but I am wondering if the memory allocated by the whole object will be freed? I assume also that calling delete on a Derived pointer will free the whole memory space.

而且,如果它不释放那部分记忆,那么它如何在同一个案件中开展工作,而是与基地班级的虚拟脱轨者一道工作,知道有多少记忆可以免费?

例:

class Base {
  public:
    int a;
    int b;
   Base() {}
  ~Base() {}
};

class Derived : public Base {
  public:
    int c;
    int d;
    Derived() {}
    ~Derived() {}
};

int main() {
  Base *p = new Derived();
  delete p; // is memory allocated for Derived freed?
}
最佳回答

它的行为不明确,因此任何事情都会发生。 标准[精选]:

In the first alternative (delete object), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined.

做不到这一点。


我认为,了解实际发生的情况是个好主意,以便理解标准为何有这一要求。

www.un.org/Depts/DGACM/index_spanish.htm 简而言之,,typical 执行,记忆将免费。 这是因为,<代码>operator 删除。 由于<代码>Base是<编码>编码>的第一个非虚拟基类,因此,该基类位于分配的记忆组首。 自<条码>起 它必须了解自己簿记所分配的区块的大小(它所知道的C级功能与类型大小无关),它将处理整个区块的位置。

但是,由于<条码>Base > 确实有过脱轨器,delete p根据静态类型的<条码>*p(即<条码>)。 因此,正如你似乎知道的那样,将不称作Derived<>/code>/strong>。 如果Derived有任何非属人成员或基类成员,这些成员或人员不会被拆解,那么y管理的任何资源都将被泄露。

当班级有虚拟脱轨器时,释放记忆的工作委托给下士。 原因是,尽管<条码>免费<>/代码>知道要确定区块的大小,但仍需要先点。 <代码>Base可在一般情况下在<编码>上任意抵消,因此Derived destructor将负责在公布之前调整点。 这还进一步表明,需要一个单独的脱轨器,在实际上不释放记忆的情况下将物体放下;例如,作为另一种衍生物的子目标,或作为明确的脱轨器。 并可在讨论中保留<代码>delete[]。 当你把你的主子作为虚拟人物时,编辑们会把这一切交给你。

底线是,标准t对所有这些执行细节做了说明,而是将它们留给——执行。 在拆除非虚拟基地时,他们可以制定一些明智的标准;但是,他们却带着简单明了的“你有虚拟的脱轨器,然后 s,否则,它不会ok”。

问题回答

从技术上讲,答案是不明的。 删除从点子到没有虚拟脱轨器的基点,是不明确的。

然而,实际上,大多数执行情况只是未能适当删除衍生目标中的不足。

从形式上讲,这是不明确的行为,因此,你没有保证记忆是免费的,或确实,你的节目是特别的。 它可以安排硬盘。 不得。 在实践中,在这种情况下,记忆很可能是免费的——但是,如果你依靠,你就会重新开张,你也不要这样做。 这简便。

Edit: I Was misn: Key prose from draft standard, Focusmine: Section 5.3.5

3 In the first alternative (delete object), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.

There is a good chance the memory will be correctly freed. Just because the Standard doesn t assure this doesn t mean it won t work. It s hard to see how it could fail.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?