English 中文(简体)
Pointer-to-Pointer坠毁案
原标题:Pointer-to-Pointer crashes in case where Pointer doesn t
  • 时间:2010-12-17 20:23:40
  •  标签:
  • c++
  • windows

我把一些密码带往Windows,并 st。 某些法典在发射时自动生效,以复制点人到点人,如果点人没有被撤销,则会再次退出点人。

我已经建立了一个样本方案,复制行为。

int main()
{
  // Pointer to a Pointer, current crash.
  InterfaceClass** ptrptr;
  ConcreteTwo* object = new ConcreteTwo();
  ptrptr = (InterfaceClass**)(&object); // cast is required for some reason.
  delete *ptrptr; // Crash here.

  // Single pointer, works fine.
  InterfaceClass* ptrptr;
  ConcreteTwo* object = new ConcreteTwo();
  ptrptr = object;
  delete ptrptr;

  // There are other cases where there are only 3 classes in the hierarchy.
  // This also works fine.
  InterfaceClass** ptrptr;
  ConcreteOne* object = new ConcreteOne();
  ptrptr = (InterfaceClass**)(&object);
  delete *ptrptr;

  return 0;
}

等级为Hierarchy。 基础课程是某种纯虚拟功能的接口,许多课程都包含在课程中,因此许多物体可能从一个以上地点继承。 由于这一具体实施,必须将其扩大到“公共虚拟界面”。 在这一例子中,“虚拟”解决了坠毁问题。

class InterfaceClass {
public:
    virtual ~InterfaceClass() {};
    InterfaceClass() {}
};

class ConcreteClass : public virtual InterfaceClass {
public:
  ConcreteClass() { }
  virtual ~ConcreteClass() {}
};

class ConcreteOne : public ConcreteClass
{
public:
  ConcreteOne(void) {}
  virtual ~ConcreteOne(void) {}
};

class ConcreteTwo : public ConcreteOne
{
public:
  ConcreteTwo(void) {}
  virtual ~ConcreteTwo(void) {}
};
最佳回答

因此,你很熟悉这样的事实,即点名的类型与点名的类型几乎没有任何关系? 换言之,如果你认为T1继承了T2,T1*也继承T2* ? 这是错误的。 现在,这如何适用于你目前的状况?

  InterfaceClass** ptrptr;
  ConcreteTwo* object = new ConcreteTwo();
  ptrptr = (InterfaceClass**)(&object); // cast is required for some reason.

这里是一个主要的问题,是陈风化。 因此,它节省了一些横向空间,但你甚至知道你刚才所投的东西是什么? 这并不是你认为的。 实际上,你从具体Two* 型到无关的间谍* 进行了播映。 现在,点人的讲话与你所说的话毫不相干。

然后,你将重新解释的点击器类型改为删除,这迅速导致你违反你自己的侧面。

问题回答

汇编者警告你,你决定以你的方式这样做。

你可以这样做:

ptrptr = (InterfaceClass**)(&object);

/code> points to 2,与不同。 InterfaceClass。 2个位于不同的地址。 <代码>pt*rptr is not a pointer to instance of InterfaceClass

请通过<代码>delete的点子是<代码>的点子。 具体Two,然而,您向汇编者表示,它是<代码>的联络点。 InterfaceClass. delete 假设该编码确实是InterfaceClass,因此坠毁。

I think the problem are in the cast lines. BTW, if you remove the cast you inserted the compiler tells you precisely what s the problem.

如果你真的想这样做,我强烈建议,你首先应设立一个临时机构:

ConcreteTwo* object = new ConcreteTwo();
InterfaceClass* ptr = object;

then you can take its address and assign it to the ptrptr variable:

InterfaceClass** ptrptr = &ptr;

now you can safely delete it:

delete *ptrptr;

考虑到<代码>ptr 在<代码>ptrptr之前,删除可能再次坠毁。

至于其余部分,Noah解释了为什么你的法典没有发挥作用。





相关问题
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?

热门标签