在VTable指针和malloc [英] On VTable pointers and malloc

查看:115
本文介绍了在VTable指针和malloc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有任何编译器独立和语法优雅的方式设置一个vtable指针在分配malloc的对象?

Is there any compiler independent and syntactically elegant way to set a vtable pointer in an object allocated with malloc?

我不能使用新的直接,因为我需要能根据需要控制内存释放的流程,这需要使用void ptrs来保存内存管理器中的内存位置,直到有足够的时间释放。

I cannot use new directly as I need to be able to control the flow of memory releases on demand which requires the use of void ptrs to hold memory locations in a memory manager until there is ample time to release.

class AbstractData
{
public:
   AbstractData() {}
   virtual ~AbstractData() {}

protected:
   virtual void SetData(int NewData) =0;
   virtual int GetData() const =0;
};

class ConcreteData : public AbstractData
{
protected:
   int Data;

public:
   ConcreteData() {}
   ~ConcreteData() {}

   void SetData(int NewData);
   int GetData() const;

};

void ConcreteData::SetData(int NewData) {Data=NewData;}

int ConcreteData::GetData() const 
{return Data;}

int main(int argc, char* argv[])
{
    int OBJ_NUMBER = 4;
    ConcreteData* Test = (ConcreteData*)malloc(OBJ_NUMBER*sizeof(ConcreteData));

    if (!Test)
        return -1;        

    for (int x = 0; x < OBJ_NUMBER; x++)
       Test[x] = ConcreteData();

    Test[0]->GetData(); //Constructor was never called, vptr never initialized, crash

    free(Test);
    Test = NULL;
}



我希望本地副本有一个初始化的vtable指针,才不是。
我知道你可以做一个编译器依赖解除vptr的偏移量,如果你知道它的位置,但这个解决方案是编译器依赖和inelegant使用跨许多分配。示例使用MSVC ++ 8.0

I was hoping the local copy would have an initialized vtable pointer, but alas it does not. I know you can do a compiler dependent dereference to the offset of the vptr if you know where it is, but this solution is compiler dependent and inelegant to use across many allocations. Example works with MSVC++ 8.0

int main(int argc, char* argv[])
{
    int OBJ_NUMBER = 4;
    ConcreteData* Test = (ConcreteData*)malloc(OBJ_NUMBER*sizeof(ConcreteData));

    if (!Test)
        return -1;        

    ConcreteData StealVPtr();

    int* VPtr = *(int**)StealVPtr; 

    for (int x = 0; x < OBJ_NUMBER; x++)
       *(int**)Test[x] = VPtr;

    Test[0]->GetData(); //VPtr initialized in compiler dependent way

    free(Test);
    Test = NULL;
}

或者,可以使用placement new,但是它再次看起来语法不雅

Alternatively, placement new could be used, but once again it looks syntactically inelegant and can cause array offsetting problems of types with destructors when it adds the array count in front of the ptr.

int main(int argc, char* argv[])
{
    int OBJ_NUMBER = 4;
    ConcreteData* Test = (ConcreteData*)malloc(OBJ_NUMBER*sizeof(ConcreteData));

    if (!Test)
        return -1;        

    for (int x = 0; x < OBJ_NUMBER; x++)
    {
        if (!(ConcreteData* PlcTest = new(Test[x]) ConcreteData()))
        {
           free(Test);
           Test = NULL;
           return -1;
        }
    }

    PlcTest[0]->GetData(); //Constructor was invoked and VPtr was initialized

    for (int x = OBJ_NUMBER-1; x >= 0; x--)
        PlcTest[x].~ConcreteData();

    PlcTest = NULL;

    free(Test);
    Test = NULL;
}

这些是真正唯一的方法来初始化对象的VTable ptr /使用malloc?

Are these really the only ways to initialize the VTable ptr/call constructors on objects using malloc?

推荐答案

您可能不知道您可以覆写全局 new delete 。注意,您还需要重写 new [] delete [] 才能完成

You may not know that you can override global new and delete. Pay attention, you also need to override new[] and delete[] to be complete

这里有一个例子:

void * operator new( size_t size ) 
{
    return super_malloc( size );
}

这篇关于在VTable指针和malloc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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