0

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

问题是我最终得到一个访问冲突异常,试图读取指向 Base 的 vtable 的指针(在 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;
}

此行为仅在通过 HeapAlloc 或 malloc 分配内存时发生;如果使用 new[],它工作正常。(此外,之前调用了 4 次 cstor,因此输出为 4 5 6 7。)

4

3 回答 3

9

如果您在没有分配内存new的情况下总是需要使用放置 new手动调用构造函数并使用析构函数x->~Derived();

于 2010-01-11T19:55:51.633 回答
1

如果你想使用 C++ 默认值之外的分配器,你应该定义你自己的 operator new 而不是每次都记住调用构造函数。

void *operator new[]( size_t block_size, HANDLE heap ) {
    return HeapAlloc( heap, 0, block_size );
}

…</p>

Derived *pDerived = new( GetProcessHeap() ) Derived[ count ];

细节取决于您是否希望它成为默认的分配方式Derived以及它是否真的需要参数。

free()如果无法释放您获得的内存,您仍然需要小心。然后默认设置delete将不起作用,您应该创建Derived::operator delete或编写自己的函数来调用object->~Derived().

于 2010-01-11T20:23:00.843 回答
0

我认为在第一个 for 循环中,您正在创建一个没有 new 的对象。这意味着这个对象的上下文是你的 for 循环。当您退出 for 循环时,此变量不再存在。

于 2010-01-11T19:55:59.980 回答