I'm trying to understand std::vector, and I know that if vector is a vector containing user-defined classes it's important that it calls its destructors in case that class has "ownership" of a resource. In this case I imagine that it would loop through each element and call the destructor on each.
我试图理解std :: vector,我知道如果vector是一个包含用户定义类的向量,那么在类具有资源“所有权”的情况下调用它的析构函数非常重要。在这种情况下,我想它会遍历每个元素并在每个元素上调用析构函数。
But in the case of plain old data, I don't think destructors would need to be called, only a simple delete of the entire block. If I had to guess I'd say this would be faster for say, reallocations.
但是在普通旧数据的情况下,我不认为需要调用析构函数,只需要简单地删除整个块。如果我不得不猜测我会说这会更快说,重新分配。
Would it be fairly easy to implement your own array class which distinguishes these two and behaves different according to what needs to be done? Thanks.
是否可以相当容易地实现自己的数组类,区分这两个数组并根据需要做什么表现不同?谢谢。
1 个解决方案
#1
1
From gcc vector implementation:
从gcc矢量实现:
void
clear()
{ _M_erase_at_end(this->_M_impl._M_start); }
So, clear calls protected function _M_erase_at_end, lets, look at it:
所以,清除调用protected函数_M_erase_at_end,让我们来看看:
void
_M_erase_at_end(pointer __pos)
{
std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
this->_M_impl._M_finish = __pos;
}
And it calls std::_Destroy from beginning of vector (__pos is beginning) till the end.
并且它从向量的开头调用std :: _ Destroy(__pos开始)直到结束。
template<typename _ForwardIterator, typename _Allocator>
void
_Destroy(_ForwardIterator __first, _ForwardIterator __last,
_Allocator& __alloc)
{
typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
for (; __first != __last; ++__first)
__traits::destroy(__alloc, std::__addressof(*__first));
}
So, it's iterated all over internal array and call __traits::destroy, not freeing all memory block at once.
因此,它遍及内部数组并调用__traits :: destroy,而不是立即释放所有内存块。
EDIT: you wrote But it would seem when clearing vector that it does call delete on its elements
- the answer is big NO. STL containers do not OWN externally allocated memory, you always must call delete
or use shared\unique_ptr or it will be leak.
编辑:你写了但是它似乎在清除矢量时它确实在其元素上调用了删除 - 答案很重要。 STL容器没有OWN外部分配的内存,你总是必须调用delete或者使用shared \ unique_ptr或者它会泄漏。
#1
1
From gcc vector implementation:
从gcc矢量实现:
void
clear()
{ _M_erase_at_end(this->_M_impl._M_start); }
So, clear calls protected function _M_erase_at_end, lets, look at it:
所以,清除调用protected函数_M_erase_at_end,让我们来看看:
void
_M_erase_at_end(pointer __pos)
{
std::_Destroy(__pos, this->_M_impl._M_finish, _M_get_Tp_allocator());
this->_M_impl._M_finish = __pos;
}
And it calls std::_Destroy from beginning of vector (__pos is beginning) till the end.
并且它从向量的开头调用std :: _ Destroy(__pos开始)直到结束。
template<typename _ForwardIterator, typename _Allocator>
void
_Destroy(_ForwardIterator __first, _ForwardIterator __last,
_Allocator& __alloc)
{
typedef __gnu_cxx::__alloc_traits<_Allocator> __traits;
for (; __first != __last; ++__first)
__traits::destroy(__alloc, std::__addressof(*__first));
}
So, it's iterated all over internal array and call __traits::destroy, not freeing all memory block at once.
因此,它遍及内部数组并调用__traits :: destroy,而不是立即释放所有内存块。
EDIT: you wrote But it would seem when clearing vector that it does call delete on its elements
- the answer is big NO. STL containers do not OWN externally allocated memory, you always must call delete
or use shared\unique_ptr or it will be leak.
编辑:你写了但是它似乎在清除矢量时它确实在其元素上调用了删除 - 答案很重要。 STL容器没有OWN外部分配的内存,你总是必须调用delete或者使用shared \ unique_ptr或者它会泄漏。