使用malloc代替new,并在创建对象时调用复制构造函数
我想尝试 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
从
malloc
获取原始内存后,您需要使用placement new
。当您完成该对象后,您必须确保显式调用其析构函数。
You'll need to use
placement new
after getting the raw memory frommalloc
.When you're done with the object you have to make sure to explicitly call its destructor.
使用新展示位置
Use placement new
allocate()
的参数是对象的数量,而不是字节大小。然后,您可以调用分配器的
construct()
函数来构造该对象。如果想将其与标准库容器或其他 std 分配器感知类型一起使用,只需为其指定分配器类型即可。
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.If want to use it with a standard library container or other std allocator aware type, simply give it the allocator type.