Reputation: 45
I have read that, in c++ STL's vector gets deallocated as soon as the variable goes out of scope. 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.
Upvotes: 3
Views: 2132
Reputation: 32532
I have read that, in c++ STL's vector gets deallocated as soon as the variable goes out of scope.
Your search keywords are object lifetime, a variable's scope and storage duration.
This has nothing to do with std::vector
specifically. If the control flow leaves the current scope, any object whose lifetime is bound to that scope (via automatic storage duration) is destroyed automatically.
For reference:
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.
The standard library containers manage the memory they allocate and the objects they create via allocators. (But I believe that these details aren't really important for the general understanding of the basic functionality.)
For reference:
Upvotes: 2
Reputation: 151
std::vector uses a destructor which calls std::vector::_Tidy
(MSVC implementation) on destruction (or deletion). This happens when the vector goes out of scope or if the vector is being destructed manually.
Upvotes: -1
Reputation: 8796
The std::vector
class's exact destruction place is left to implementation by spec.
But usually (when the vector goes out of scope), it should result in calling std::allocator
's deallocate
method (which is std::vector
's second template-arg).
See also What's the advantage of using std::allocator
instead of new in C++?
Actually, remember that there is no rule for standard classes to be actual files (meaning you can ignore what you see there safely, as long as your code follows C++ standards, because those are part of compiler not C++).
Upvotes: 3
Reputation: 6326
There are several STL implementations, they diff from each other, but they are similar. Let's take the latest version of GCC's libstdc++ as an example:
The destructor of std::vector
, all the elements in the vector are destroyed via calling 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;
}
The vector
has a base class:
template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
class vector : protected _Vector_base<_Tp, _Alloc>
For the memory used in the vector itself, it is destroyed in _M_deallocate
in the base class, which will call the deallocate
at last:
~_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
is a macro, we can regard it as delete
Upvotes: 4
Reputation: 596843
The vector's destructor destroys the dynamic memory and its content which the vector has created during its lifetime.
When the vector goes out of scope, its destructor is run automatically as part of its own destruction.
Upvotes: 2