Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

vector dealocation in a destructor

23 views
Skip to first unread message

Popping mad

unread,
Nov 6, 2016, 9:04:42 PM11/6/16
to

Since we can't just delete a vector, do I need to deallocate it when we
call a destructor? I think I do, but I don't know how.

Öö Tiib

unread,
Nov 7, 2016, 12:09:37 AM11/7/16
to
On Monday, 7 November 2016 04:04:42 UTC+2, Popping mad wrote:
> Since we can't just delete a vector, do I need to deallocate it when we
> call a destructor? I think I do, but I don't know how.

It feels that I misunderstand what you are asking. We call
a destructor of object manually on very rare circumstances. Destructor
is called automatically when the lifetime of an object ends. So manually
we call it on case when we want to hack with these life-times with
placement news and such.

Christopher J. Pisz

unread,
Nov 7, 2016, 12:35:08 AM11/7/16
to
On 11/6/2016 8:04 PM, Popping mad wrote:
>
> Since we can't just delete a vector, do I need to deallocate it when we
> call a destructor? I think I do, but I don't know how.
>

I am not sure what you are referring to when you say "deallocate it" It
is best to not use any pronouns at all, imo, when talking programmin.

Like every class, std::vector has its own deconstructor that is called
when it goes out of scope. The deconstructor will clean up any memory
the vector used internally.

However, if the objects the vector contains, used any memory themselves,
then those objects are responsible for cleaning up that memory.

If you allocated things yourself and put those things into the vector,
then you are going to be responsible for releasing them as well.




ruben safir

unread,
Nov 7, 2016, 1:15:24 AM11/7/16
to
thanks

Popping mad

unread,
Nov 7, 2016, 1:21:52 AM11/7/16
to
On Sun, 06 Nov 2016 21:09:15 -0800, Öö Tiib wrote:

> It feels that I misunderstand what you are asking.

Well, I am building the destructor for a class which has large vectors
and so I was thinking I needed to manually delete them within the
destructor. But if I understand correctly, vectors will trigger their
own destructor when they reach the end of their life.

Öö Tiib

unread,
Nov 7, 2016, 1:58:45 AM11/7/16
to
Yes. Most C++ programmers follow RAII idiom and everything in standard
library supports it so if we did not allocate memory manually
(like with 'new', 'malloc' or [yuck] 'strdup') then we don't need
to manually deallocate it.

Note that if you did actually allocate your vector with 'new' because
you have raw pointer to vector member then you have one level of
indirection too many and should refactor it to just vector data member
to get rid of that 'new'.
0 new messages