安全放置新&显式析构函数调用

发布于 2024-09-04 05:58:19 字数 802 浏览 10 评论 0原文

这是我的代码的示例:

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;
    }
}

我的问题是,这是一种安全的方法吗?在某些情况下我会犯一些隐藏的错误吗?它适用于所有类型的对象(PO​​D 和非 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 技术交流群。

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

发布评论

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

评论(4

关于从前 2024-09-11 05:58:19

不,因为构造函数和析构函数都被调用了两次。因为你有这个:

template <typename T> struct MyStruct {
    T object;
}

当你构造 MyStruct 时,编译将构造内部 T ,当你删除对象时,内部 T将自动调用析构函数。

对于此示例,不需要放置 new 或显式析构函数调用。

如果您分配原始内存,则放置 new 会很有用。例如,如果您将 new 更改为:

this->structPool  = new char[sizeof(T) * count];

那么您将需要放置 new 和显式析构函数调用。

No, because both your constructor and destructor are invoked twice. Because you have this:

template <typename T> struct MyStruct {
    T object;
}

When you construct a MyStruct<T> the compile will construct the inner T and when you delete the object the inner T 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:

this->structPool  = new char[sizeof(T) * count];

then you would want to placement new and explict destructor call.

雅心素梦 2024-09-11 05:58:19

不,这肯定不是一种安全的方法。当您对非 POD T 执行 new MyStruct[count] 时,数组中的每个 MyStruct 对象已获得默认值-constructed,意味着object成员的构造函数被自动调用。然后,您尝试在此基础上执行就地构造(通过值初始化)。由此产生的行为是未定义的。

删除也存在同样的问题。

你想达到什么目的?只需执行 new MyStruct[count]() (注意额外的空 ()),它就已经为数组的每个元素执行值初始化(确切地说之后您要“手动”执行的操作)。为什么你觉得必须就地施工?

同样,当您执行

delete[] this->structPool;

此操作时,它会自动为数组中的每个 MyStruct::object 成员调用析构函数。无需手动执行。

No, it is certainly not even remotely safe way to do it. When you do new MyStruct<T>[count] for non-POD T, each MyStruct<T> object in the array already gets default-constructed, meaning that the constructor for the object 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

delete[] this->structPool;

it automatically calls the destructor for each MyStruct<T>::object member in the array. No need to do it manually.

别理我 2024-09-11 05:58:19
  1. 记住new总是会调用构造函数,无论它是否是放置的。

-- 所以你的代码使用了 new 两次。这将调用构造函数两次。如果你想避免这种情况,可以:

将第一个 new 更改为 malloc(或任何类型的 alloc)

删除第二个放置 new

  1. 要删除数组中的对象,最好的方法是: 调用每个对象的析构函数;释放内存。

-- 因此,您可以执行以下任一操作:

如果您使用 new[],则使用 delete[] 删除对象 如果

您使用 malloc 和放置 new,则调用每个析构函数并执行 C 风格的 free

  1. Remembering new will always call the constructor, no matter if it is placement or not.

-- 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

  1. To delete the objects in an array, the best way is: Invoke every object's destructor; free the memory.

-- 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

ゝ偶尔ゞ 2024-09-11 05:58:19
template <typename T> class MyClass {
    void* structPool;
    size_t structCount;

    MyClass(size_t count)
      : structPool(new char[sizeof(T)*count], structCount(count)
    {
        //placement new to call constructor
        for( size_t i=0 ; i<count ; i++ )
            new (structPool+i*sizeof(T)) T(); 
    }

    ~MyClass() {
        //explicit destructor call
        for( size_t i=0 ; i<structCount ; i++ )
            reinterpret_cast<T*>(structPool+i*sizeof(T))->~T(); 
        delete[] structPool;
    }
}

请注意,这不是异常安全的:如果其中一个构造函数导致异常,它不会对已构造的对象调用析构函数,并且会泄漏内存。当其中一个析构函数抛出异常时,这也会失败。

查看您最喜欢的 std lib 实现中的 std::vector ,了解如何正确执行此操作。然而,这引出了一个问题:你为什么要这样做?
std::vector 已经完成了这一切,并且做得正确,您可以开箱即用地使用它,并且每个查看您的代码的人都会立即理解它:

template <typename T> class MyClass {
    std::vector<T> data_;
    MyClass(size_t count) : data() {data.resize(count);}
    //~MyClass() not needed
}
template <typename T> class MyClass {
    void* structPool;
    size_t structCount;

    MyClass(size_t count)
      : structPool(new char[sizeof(T)*count], structCount(count)
    {
        //placement new to call constructor
        for( size_t i=0 ; i<count ; i++ )
            new (structPool+i*sizeof(T)) T(); 
    }

    ~MyClass() {
        //explicit destructor call
        for( size_t i=0 ; i<structCount ; i++ )
            reinterpret_cast<T*>(structPool+i*sizeof(T))->~T(); 
        delete[] structPool;
    }
}

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:

template <typename T> class MyClass {
    std::vector<T> data_;
    MyClass(size_t count) : data() {data.resize(count);}
    //~MyClass() not needed
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文