我应该自己擦除boost :: ptr_vector的指针吗?

时间:2022-11-15 19:55:56

I was wondering if this code leak :

我想知道这个代码是否泄漏:

int main()
{
boost::ptr_vector <char> v;
v.push_back(new char[10]);
v.clear()
}

int main(){boost :: ptr_vector v; v.push_back(new char [10]); v.clear()}

Will the ptr_vector destructor or clear() function delete the pointers it contains or do i have to do it myself?

请问ptr_vector析构函数或clear()函数是否会删除它包含的指针,或者我必须自己执行此操作吗?

1 个解决方案

#1


-3  

From the vector documentation (http://www.cplusplus.com/reference/stl/vector/~vector/):

从矢量文档(http://www.cplusplus.com/reference/stl/vector/~vector/):

Vector destructor

矢量析构函数

Destructs the container object. This calls each of the contained element's destructors, and deallocates all the storage capacity allocated by the vector.

破坏容器对象。这将调用每个包含元素的析构函数,并释放向量分配的所有存储容量。

delete[] won't be called, so it'll leak. And as other commenters pointed out, there are more STL ways to do it.

delete []不会被调用,所以它会泄漏。正如其他评论者指出的那样,有更多的STL方法可以做到这一点。

#1


-3  

From the vector documentation (http://www.cplusplus.com/reference/stl/vector/~vector/):

从矢量文档(http://www.cplusplus.com/reference/stl/vector/~vector/):

Vector destructor

矢量析构函数

Destructs the container object. This calls each of the contained element's destructors, and deallocates all the storage capacity allocated by the vector.

破坏容器对象。这将调用每个包含元素的析构函数,并释放向量分配的所有存储容量。

delete[] won't be called, so it'll leak. And as other commenters pointed out, there are more STL ways to do it.

delete []不会被调用,所以它会泄漏。正如其他评论者指出的那样,有更多的STL方法可以做到这一点。