安全放置新&显式析构函数调用
这是我的代码的示例:
template <typename T> struct MyStruct {
T object;
}
template <typename T> class MyClass {
MyStruct<T>* structPool;
size_t structCount;
MyClass(size_t count) {
this->structCount = count;
this->structPool = new MyStruct<T>[count];
for( size_t i=0 ; i<count ; i++ ) {
//placement new to call constructor
new (&this->structPool[i].object) T();
}
}
~MyClass() {
for( size_t i=0 ; i<this->structCount ; i++ ) {
//explicit destructor call
this->structPool[i].object.~T();
}
delete[] this->structPool;
}
}
我的问题是,这是一种安全的方法吗?在某些情况下我会犯一些隐藏的错误吗?它适用于所有类型的对象(POD 和非 POD)吗?
This is an example of my codes:
template <typename T> struct MyStruct {
T object;
}
template <typename T> class MyClass {
MyStruct<T>* structPool;
size_t structCount;
MyClass(size_t count) {
this->structCount = count;
this->structPool = new MyStruct<T>[count];
for( size_t i=0 ; i<count ; i++ ) {
//placement new to call constructor
new (&this->structPool[i].object) T();
}
}
~MyClass() {
for( size_t i=0 ; i<this->structCount ; i++ ) {
//explicit destructor call
this->structPool[i].object.~T();
}
delete[] this->structPool;
}
}
My question is, is this a safe way to do? Do I make some hidden mistake at some condition? Will it work for every type of object (POD and non-POD)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不,因为构造函数和析构函数都被调用了两次。因为你有这个:
当你构造
MyStruct
时,编译将构造内部T
,当你删除对象时,内部T
将自动调用析构函数。对于此示例,不需要放置 new 或显式析构函数调用。
如果您分配原始内存,则放置 new 会很有用。例如,如果您将 new 更改为:
那么您将需要放置 new 和显式析构函数调用。
No, because both your constructor and destructor are invoked twice. Because you have this:
When you construct a
MyStruct<T>
the compile will construct the innerT
and when you delete the object the innerT
will have the destructor called automatically.For this example, there is no need for placement new or an explicit destructor call.
Placement new would be useful if you allocate raw memory. For example, if you changed your new to:
then you would want to placement new and explict destructor call.
不,这肯定不是一种安全的方法。当您对非 POD
T
执行new MyStruct[count]
时,数组中的每个MyStruct
对象已获得默认值-constructed,意味着object
成员的构造函数被自动调用。然后,您尝试在此基础上执行就地构造(通过值初始化)。由此产生的行为是未定义的。删除也存在同样的问题。
你想达到什么目的?只需执行
new MyStruct[count]()
(注意额外的空()
),它就已经为数组的每个元素执行值初始化(确切地说之后您要“手动”执行的操作)。为什么你觉得必须就地施工?同样,当您执行
此操作时,它会自动为数组中的每个
MyStruct::object
成员调用析构函数。无需手动执行。No, it is certainly not even remotely safe way to do it. When you do
new MyStruct<T>[count]
for non-PODT
, eachMyStruct<T>
object in the array already gets default-constructed, meaning that the constructor for theobject
member gets called automatically. Then you attempt to perform an in-place construction (by value-initialization) on top of that. The resultant behavior is undefined.The same problem exists with the deletion.
What is it you are trying to achieve? Just do
new MyStruct<T>[count]()
(note the extra empty()
) and it will already perform value-initialization for each element of the array (exactly what you are trying to do "manually" afterwards). Why do you feel you have to do it by in-place construction?Likewise, when you do
it automatically calls the destructor for each
MyStruct<T>::object
member in the array. No need to do it manually.-- 所以你的代码使用了 new 两次。这将调用构造函数两次。如果你想避免这种情况,可以:
将第一个 new 更改为 malloc(或任何类型的 alloc)
删除第二个放置 new
-- 因此,您可以执行以下任一操作:
如果您使用 new[],则使用 delete[] 删除对象 如果
您使用 malloc 和放置 new,则调用每个析构函数并执行 C 风格的 free
-- So your code used new twice. This will call the constructor twice. If you want to avoid this, either:
Change your first new into malloc(or any kind of alloc)
Remove your second placement new
-- So you can do either:
Delete the object with delete[] if you are using new[]
Call every destructor and do a C style free if you are using malloc and placement new
请注意,这不是异常安全的:如果其中一个构造函数导致异常,它不会对已构造的对象调用析构函数,并且会泄漏内存。当其中一个析构函数抛出异常时,这也会失败。
查看您最喜欢的 std lib 实现中的
std::vector
,了解如何正确执行此操作。然而,这引出了一个问题:你为什么要这样做?std::vector
已经完成了这一切,并且做得正确,您可以开箱即用地使用它,并且每个查看您的代码的人都会立即理解它:Note that this isn't exception-safe: If one of the constructors causes an exception, it doesn't call the destructors on the objects already constructed and it leaks the memory. When one of the destructors throws, this fails, too.
Have a look at
std::vector
in your favorite std lib implementation in order to see how to do this right. However, that leads to the question: Why do you want to do this in the first place?A
std::vector
already does all this, does it right, you can use it out of the box, and everyone looking at your code will understand it immediately: