在没有new []的情况下分配Derived数组:指向Base vtable的指针很糟糕

ian*_*ano 0 c++ inheritance vtable dynamic-memory-allocation

基本上,我有一个纯虚拟类Base,以及一个继承自Base的具体类Derived.然后我分配一块内存并通过简单的转换将其视为Derived数组.然后,我使用=填充数组.最后,我循环遍历数组,尝试调用在Base中声明并在Derived中定义的虚方法GetIndex.

问题是我最终得到一个访问冲突异常试图读取指向vtable for Base的指针(在Visual Studio调试中,这显示为__vfptr,它始终是0xbaadf00d).

以下是我遇到的问题的一个简单示例:

#include "stdafx.h"
#include "windows.h"

struct Base
{
    virtual int GetIndex() const = 0;
};

struct Derived : public Base
{
    int index;

    Derived()
    {
        static int test = 0;
        index = test++;
    }

    int GetIndex() const
    {
        return index;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    int count = 4;
    // Also fails with malloc
    Derived* pDerived = (Derived*)HeapAlloc(GetProcessHeap(), 0, sizeof(Derived) * count);

    for (int i = 0; i < count; i++)
    {
        Derived t;
        pDerived[i] = t;
    }

    // Should print 0 1 2 3
    for (int i = 0; i < count; i++)
    {
        Base& lc = pDerived[i];
        printf("%d\n", lc.GetIndex()); // FAIL!
    }
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

仅当通过HeapAlloc或malloc分配内存时才会出现此问题; 如果使用new [],它可以正常工作.(此外,cstor先前被调用了4次,因此输出为4 5 6 7.)

ken*_*ytm 9

如果你没有分配内存,new你总是需要使用placement new和析构函数手动调用构造函数x->~Derived();