使用malloc代替new,并在创建对象时调用复制构造函数

发布于 2024-10-16 22:26:42 字数 1511 浏览 3 评论 0原文

我想尝试 TBB 的可扩展分配器,但当我不得不替换一些代码时感到困惑。 这是使用分配器完成分配的方式:

SomeClass* s = scalable_allocator<SomeClass>().allocate( sizeof(SomeClass) );

编辑:上面显示的内容并不是使用可伸缩_分配器完成分配的方式。作为 ymett正确提到了,分配是这样完成的:

int numberOfObjectsToAllocateFor = 1;
SomeClass* s = scalable_allocator<SomeClass>().allocate( numberOfObjectsToAllocateFor );
scalable_allocator<SomeClass>().construct( s, SomeClass());
scalable_allocator<SomeClass>().destroy(s);
scalable_allocator<SomeClass>().deallocate(s, numberOfObjectsToAllocateFor);

这非常像使用malloc:

SomeClass* s = (SomeClass*) malloc (sizeof(SomeClass));

这是我想要替换的代码:

SomeClass* SomeClass::Clone() const
{
   return new SomeClass(*this);
}//Clone

所以尝试了一个程序:

#include<iostream>
#include<cstdlib>
using namespace std;

class S
{
        public:
        int i;
        S() {cout<<"constructed"<<endl;}
        ~S() {cout<<"destructed"<<endl;}
        S(const S& s):i(s.i) {}
};

int main()
{
        S* s = (S*) malloc(sizeof(S));
        s = (S*) S();//this is obviously wrong
        free(s);
}

在这里我发现调用malloc不会实例化对象(我之前从未使用过 malloc)。因此,在弄清楚如何将 *this 传递给复制构造函数之前,我想知道如何在使用 malloc 时实例化对象。

I wanted to try out TBB's scalable_allocator, but was confused when I had to replace some of my code.
This is how allocation is done with the allocator:

SomeClass* s = scalable_allocator<SomeClass>().allocate( sizeof(SomeClass) );

EDIT: What's shown above is not how allocation is done with scalable_allocator. As ymett correctly mentioned, allocation is done like this:

int numberOfObjectsToAllocateFor = 1;
SomeClass* s = scalable_allocator<SomeClass>().allocate( numberOfObjectsToAllocateFor );
scalable_allocator<SomeClass>().construct( s, SomeClass());
scalable_allocator<SomeClass>().destroy(s);
scalable_allocator<SomeClass>().deallocate(s, numberOfObjectsToAllocateFor);

It's pretty much like using a malloc:

SomeClass* s = (SomeClass*) malloc (sizeof(SomeClass));

This is the code I wanted to replace:

SomeClass* SomeClass::Clone() const
{
   return new SomeClass(*this);
}//Clone

So tried a program:

#include<iostream>
#include<cstdlib>
using namespace std;

class S
{
        public:
        int i;
        S() {cout<<"constructed"<<endl;}
        ~S() {cout<<"destructed"<<endl;}
        S(const S& s):i(s.i) {}
};

int main()
{
        S* s = (S*) malloc(sizeof(S));
        s = (S*) S();//this is obviously wrong
        free(s);
}

and here I found that calling malloc does not instantiate the object (I've never used malloc earlier). So before figuring out how to pass *this to the copy ctor, I'd like to know how to instantiate the object when working with malloc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你的他你的她 2024-10-23 22:26:42

malloc 获取原始内存后,您需要使用 placement new

void* mem = malloc(sizeof(S));
S* s = new (mem) S(); //this is the so called "placement new"

当您完成该对象后,您必须确保显式调用其析构函数。

s->~S();
free(mem);

You'll need to use placement new after getting the raw memory from malloc.

void* mem = malloc(sizeof(S));
S* s = new (mem) S(); //this is the so called "placement new"

When you're done with the object you have to make sure to explicitly call its destructor.

s->~S();
free(mem);
小帐篷 2024-10-23 22:26:42

使用新展示位置

#include <memory>
//...
int main()
{
        S* s = (S*) malloc(sizeof(S));
        s = new (s) S();//placement new
        //...
        s->~S();
        free(s);
}

Use placement new

#include <memory>
//...
int main()
{
        S* s = (S*) malloc(sizeof(S));
        s = new (s) S();//placement new
        //...
        s->~S();
        free(s);
}
昔日梦未散 2024-10-23 22:26:42

allocate() 的参数是对象的数量,而不是字节大小。
然后,您可以调用分配器的 construct() 函数来构造该对象。

scalable_allocator<SomeClass> sa;
SomeClass* s = sa.allocate(1);
sa.construct(s, SomeClass());
// ...
sa.destroy(s);
sa.deallocate(s);

如果想将其与标准库容器或其他 std 分配器感知类型一起使用,只需为其指定分配器类型即可。

std::vector<SomeClass, scalable_allocator<SomeClass>> v;

The parameter to allocate() is the number of objects, not the size in bytes.
You then call the allocator's construct() function to construct the object.

scalable_allocator<SomeClass> sa;
SomeClass* s = sa.allocate(1);
sa.construct(s, SomeClass());
// ...
sa.destroy(s);
sa.deallocate(s);

If want to use it with a standard library container or other std allocator aware type, simply give it the allocator type.

std::vector<SomeClass, scalable_allocator<SomeClass>> v;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文