Yashaswi
Yashaswi

Reputation: 45

How does std::vector in c++ gets deallocated by default

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

Answers (5)

moooeeeep
moooeeeep

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

XvXLuka222
XvXLuka222

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

Top-Master
Top-Master

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

prehistoricpenguin
prehistoricpenguin

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

Remy Lebeau
Remy Lebeau

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

Related Questions