c ++内存分配器vptr / new实现(c++ memory allocator vptr / new implementation)

我正在为我正在进行的项目制作一个内存池分配器。 我的池分配器返回内存块,但是我的类继承自另一个类。

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); *fx = MyClass();

这不会为我设置vtable。 我做了一些搜索,发现新的操作符可以占用一大块内存。

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); fx = new(fx)MyClass();

这将初始化vptr。 所以我想知道是否有分配vptr我自己或者它是否完全取决于编译器的心血来潮?

I was making a memory pool allocator for a project I am working on. My pool allocator returns chunks of memory fine however my class inherits from another class.

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); *fx = MyClass();

This doesn't setup the vtable for me. I did some searching and found out the new operator can take in a chunk of memory.

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); fx = new(fx)MyClass();

And this will initialize the vptr. So I was wondering if there is anyway to allocate the vptr my self or is it strictly up to the compilers whim?

最满意答案

new(fx)MyClass()

这称为“placement new”,实际上会调用构造函数来实际创建一个对象。

在您分配的内存中创建该对象之前, 它根本就不存在 (作为该类型的对象),因此它只是您投射到某些内容的原始无类型内存,这会导致未定义的行为。

我建议你把你的功能变成一个模板。

template<class T> T* allocateEffect() { return new (allocate_some_memory(sizeof(T))) T(); }

添加适当的可变参数模板并转发构造函数参数留给读者练习。

new(fx)MyClass()

This is called "placement new" and will actually call a constructor to actually create an object.

Until you created that object in the memory you allocated, it simply does not exist (as an object of that type) so it is just raw untyped memory that you cast to something, which results in undefined behavior.

I would suggest that you make your function a template like.

template<class T> T* allocateEffect() { return new (allocate_some_memory(sizeof(T))) T(); }

adding proper variadic templates and forwarding for constructor arguments is left as an exercise to the reader.

c ++内存分配器vptr / new实现(c++ memory allocator vptr / new implementation)

我正在为我正在进行的项目制作一个内存池分配器。 我的池分配器返回内存块,但是我的类继承自另一个类。

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); *fx = MyClass();

这不会为我设置vtable。 我做了一些搜索,发现新的操作符可以占用一大块内存。

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); fx = new(fx)MyClass();

这将初始化vptr。 所以我想知道是否有分配vptr我自己或者它是否完全取决于编译器的心血来潮?

I was making a memory pool allocator for a project I am working on. My pool allocator returns chunks of memory fine however my class inherits from another class.

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); *fx = MyClass();

This doesn't setup the vtable for me. I did some searching and found out the new operator can take in a chunk of memory.

MyClass *fx = (MyClass*)pool->allocateEffect(sizeof(MyClass)); fx = new(fx)MyClass();

And this will initialize the vptr. So I was wondering if there is anyway to allocate the vptr my self or is it strictly up to the compilers whim?

最满意答案

new(fx)MyClass()

这称为“placement new”,实际上会调用构造函数来实际创建一个对象。

在您分配的内存中创建该对象之前, 它根本就不存在 (作为该类型的对象),因此它只是您投射到某些内容的原始无类型内存,这会导致未定义的行为。

我建议你把你的功能变成一个模板。

template<class T> T* allocateEffect() { return new (allocate_some_memory(sizeof(T))) T(); }

添加适当的可变参数模板并转发构造函数参数留给读者练习。

new(fx)MyClass()

This is called "placement new" and will actually call a constructor to actually create an object.

Until you created that object in the memory you allocated, it simply does not exist (as an object of that type) so it is just raw untyped memory that you cast to something, which results in undefined behavior.

I would suggest that you make your function a template like.

template<class T> T* allocateEffect() { return new (allocate_some_memory(sizeof(T))) T(); }

adding proper variadic templates and forwarding for constructor arguments is left as an exercise to the reader.