默认情况下,c++ 中的 std::vector 如何被释放
How does std::vector in c++ gets deallocated by default
我读到过,在 C++ 中,一旦变量超出范围,STL 的向量就会被释放。所以我尝试通过 STL 的 vector 实现,但它似乎并没有发生在 class vector 的析构函数中,那么释放是如何发生的或者它是如何实现的。
向量的析构函数销毁向量在其生命周期内创建的动态内存及其内容。
当 vector 超出范围时,其析构函数 运行 自动作为其自身析构的一部分。
std::vector 使用 析构函数 调用 std::vector::_Tidy
(MSVC 实现)销毁(或删除)。当向量超出范围或向量被手动破坏时会发生这种情况。
std::vector
class的具体销毁位置留给规范实现。
但通常(当向量超出范围时),它应该导致调用 std::allocator
的 deallocate
方法(这是 std::vector
的第二个模板参数) .
另见
实际上,请记住标准 classes 没有规则是实际文件(这意味着您可以安全地忽略在那里看到的内容,只要您的代码遵循 C++ 标准,因为它们是编译器不是 C++)。
有几种STL实现,它们彼此不同,但它们是相似的。我们以最新版本的GCC的libstdc++为例:
std::vector
的析构函数,通过调用std::_Destroy
销毁vector中的所有元素
~vector() _GLIBCXX_NOEXCEPT
{
std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
_M_get_Tp_allocator());
_GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC;
}
vector
有一个基数 class:
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class vector : protected _Vector_base<_Tp, _Alloc>
vector本身使用的内存,在基class中的_M_deallocate
中销毁,最后会调用deallocate
:
~_Vector_base() _GLIBCXX_NOEXCEPT
{
_M_deallocate(_M_impl._M_start,
_M_impl._M_end_of_storage - _M_impl._M_start);
}
void
deallocate(_Tp* __p, size_type __n __attribute__ ((__unused__)))
{
#if __cpp_sized_deallocation
# define _GLIBCXX_SIZED_DEALLOC(p, n) (p), (n) * sizeof(_Tp)
#else
# define _GLIBCXX_SIZED_DEALLOC(p, n) (p)
#endif
#if __cpp_aligned_new
if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
{
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),
std::align_val_t(alignof(_Tp)));
return;
}
#endif
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
}
_GLIBCXX_OPERATOR_DELETE
是一个宏,我们可以看成是delete
I have read that, in c++ STL's vector gets deallocated as soon as the variable goes out of scope.
您的搜索关键字是对象生命周期、变量的范围和存储持续时间。
这与std::vector
具体无关。如果控制流离开当前作用域,则生命周期绑定到该作用域(通过自动存储持续时间)的任何对象都会自动销毁。
供参考:
So I tried going through STL's vector implementation, but it doesn't seem to be happening in the destructor of class vector, so how does deallocation happen or how is it being implemented.
标准库容器管理它们分配的内存以及它们通过分配器创建的对象。 (但我相信这些细节对于基本功能的一般理解并不重要。)
供参考:
我读到过,在 C++ 中,一旦变量超出范围,STL 的向量就会被释放。所以我尝试通过 STL 的 vector 实现,但它似乎并没有发生在 class vector 的析构函数中,那么释放是如何发生的或者它是如何实现的。
向量的析构函数销毁向量在其生命周期内创建的动态内存及其内容。
当 vector 超出范围时,其析构函数 运行 自动作为其自身析构的一部分。
std::vector 使用 析构函数 调用 std::vector::_Tidy
(MSVC 实现)销毁(或删除)。当向量超出范围或向量被手动破坏时会发生这种情况。
std::vector
class的具体销毁位置留给规范实现。
但通常(当向量超出范围时),它应该导致调用 std::allocator
的 deallocate
方法(这是 std::vector
的第二个模板参数) .
另见
实际上,请记住标准 classes 没有规则是实际文件(这意味着您可以安全地忽略在那里看到的内容,只要您的代码遵循 C++ 标准,因为它们是编译器不是 C++)。
有几种STL实现,它们彼此不同,但它们是相似的。我们以最新版本的GCC的libstdc++为例:
std::vector
的析构函数,通过调用std::_Destroy
~vector() _GLIBCXX_NOEXCEPT
{
std::_Destroy(this->_M_impl._M_start, this->_M_impl._M_finish,
_M_get_Tp_allocator());
_GLIBCXX_ASAN_ANNOTATE_BEFORE_DEALLOC;
}
vector
有一个基数 class:
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class vector : protected _Vector_base<_Tp, _Alloc>
vector本身使用的内存,在基class中的_M_deallocate
中销毁,最后会调用deallocate
:
~_Vector_base() _GLIBCXX_NOEXCEPT
{
_M_deallocate(_M_impl._M_start,
_M_impl._M_end_of_storage - _M_impl._M_start);
}
void
deallocate(_Tp* __p, size_type __n __attribute__ ((__unused__)))
{
#if __cpp_sized_deallocation
# define _GLIBCXX_SIZED_DEALLOC(p, n) (p), (n) * sizeof(_Tp)
#else
# define _GLIBCXX_SIZED_DEALLOC(p, n) (p)
#endif
#if __cpp_aligned_new
if (alignof(_Tp) > __STDCPP_DEFAULT_NEW_ALIGNMENT__)
{
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n),
std::align_val_t(alignof(_Tp)));
return;
}
#endif
_GLIBCXX_OPERATOR_DELETE(_GLIBCXX_SIZED_DEALLOC(__p, __n));
}
_GLIBCXX_OPERATOR_DELETE
是一个宏,我们可以看成是delete
I have read that, in c++ STL's vector gets deallocated as soon as the variable goes out of scope.
您的搜索关键字是对象生命周期、变量的范围和存储持续时间。
这与std::vector
具体无关。如果控制流离开当前作用域,则生命周期绑定到该作用域(通过自动存储持续时间)的任何对象都会自动销毁。
供参考:
So I tried going through STL's vector implementation, but it doesn't seem to be happening in the destructor of class vector, so how does deallocation happen or how is it being implemented.
标准库容器管理它们分配的内存以及它们通过分配器创建的对象。 (但我相信这些细节对于基本功能的一般理解并不重要。)
供参考: