For Programmers: Free Programming Magazines  


Home > Archive > VC STL > January 2006 > std::vector









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author std::vector
Petar Popara

2005-12-19, 4:01 am


Do I have in this case:

vector<MyClass*> m_something;
MyClass * mc = new MyClass();
m_something.push_back(mc);

to delete objects:

for (unsigned int i=0; i<m_something.size(); i++)
delete m_something[i];

when m_something goes out of scope or vector template class will do it for
me?


Mateusz Łoskot

2005-12-19, 4:01 am

Petar Popara wrote:
> Do I have in this case:
>
> vector<MyClass*> m_something;
> MyClass * mc = new MyClass();
> m_something.push_back(mc);
>
> to delete objects:
>
> for (unsigned int i=0; i<m_something.size(); i++)
> delete m_something[i];
>
> when m_something goes out of scope or vector template class will do it for
> me?
>
>


No, you have to do it yourself. It applies to all STL containers.
Another solution is to use "smart pointers" i.e. boost::shared_ptr
to destroy objects pointed by contained pointers automatically.

I suggest you to read following books:

Effective STL by Scott Meyers (Item 5 or 7, I don't remember exactly
which one)
http://accu.org/cgi-bin/accu/rvout....m&file=e002375a

The C++ Standard Library by Nicolai Josuttis
http://accu.org/cgi-bin/accu/rvout....&file=cp002018a

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Mateusz Łoskot

2005-12-19, 4:01 am

This article may also be helpful:
http://www.cuj.com/documents/s=7990/cujcexp1910austern/

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Carl Daniel [VC++ MVP]

2006-01-09, 11:10 pm

Mateusz Loskot wrote:
> Petar Popara wrote:
>
> No, you have to do it yourself. It applies to all STL containers.
> Another solution is to use "smart pointers" i.e. boost::shared_ptr
> to destroy objects pointed by contained pointers automatically.


Wrong.

>
> I suggest you to read following books:
>
> Effective STL by Scott Meyers (Item 5 or 7, I don't remember exactly
> which one)
> http://accu.org/cgi-bin/accu/rvout....m&file=e002375a
>
> The C++ Standard Library by Nicolai Josuttis
> http://accu.org/cgi-bin/accu/rvout....&file=cp002018a


I'd suggest that you read them as well.

-cd


Carl Daniel [VC++ MVP]

2006-01-09, 11:10 pm

Petar Popara wrote:
> Do I have in this case:
>
> vector<MyClass*> m_something;
> MyClass * mc = new MyClass();
> m_something.push_back(mc);
>
> to delete objects:
>
> for (unsigned int i=0; i<m_something.size(); i++)
> delete m_something[i];
>
> when m_something goes out of scope or vector template class will do
> it for me?


Yes, in this case you have to delete the objects yourself.

The STL containers are designed to store "values", and will take care of
automatically destroying those "values" when the contrainer is destroyed.
In this case, you're storing values of type "MyClass*", which will be
automatically destroyed when the container goes out of scope.

So, what does the "destructor" for a "MyClass*" do? Precisely nothing -
just like the destructors for all built-in types.

Typically, you would simply store instances of your class by value rather
than by pointer:

std::vector<MyClass> m_somthing;
m_something.push_back(MyClass());

to do this, your type needs to be Copy Constructible and Assignable, as
defined by the C++ standard.

If you can't store your objects by value, but must use some kind of pointer
(for example, if your class is not copy constructible), then you need to
make sure the heap-allocated instances of your class get cleaned up. You
can do this as your wrote above, or you can get an automatic solution by
using a "smart pointer" class, such as boost::shared_ptr (see
www.boost.org).

std::vector<boost::shared_ptr<MyClass> > m_something;
MyClass * mc = new MyClass();
m_something.push_back(mc);

-cd



Igor Tandetnik

2006-01-09, 11:10 pm

Carl Daniel [VC++ MVP]
<cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote:
> Mateusz Loskot wrote:
>
> Wrong.


I'm . Within the same thread, you claim that boost::shared_ptr
works for this and that it doesn't. Which way is it?
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


Ulrich Eckhardt

2006-01-09, 11:10 pm

Carl Daniel [VC++ MVP] wrote:
> Mateusz Loskot wrote:
[does vector<T*> delete the contained Ts?][color=darkred]
>
> Wrong.


What exactly is it that is wrong here? I mean I could pick some minor nit
like STL != C++ standardlibrary but that's probably not what you meant...

Uli


Carl Daniel [VC++ MVP]

2006-01-09, 11:10 pm

Igor Tandetnik wrote:
> Carl Daniel [VC++ MVP]
> <cpdaniel_remove_this_and_nospam@mvps.org.nospam> wrote:
>
> I'm . Within the same thread, you claim that boost::shared_ptr
> works for this and that it doesn't. Which way is it?


I was too. Mateusz' response was correct, but the first word of
his response threw me off.

The OP was asking "do I need to delete the objects myself", to which Mateusz
replied "No", then proceeded to explain why the answer is yes and how to get
around it, just as I did in the other post. Unfortunately, the OP asked an
either/or question which cannot be answered yes/no - I parsed the No as
answering the first part of the either/or instead of the second part.

Mea culpa.

-cd


Carl Daniel [VC++ MVP]

2006-01-09, 11:10 pm

Ulrich Eckhardt wrote:
> Carl Daniel [VC++ MVP] wrote:
> [does vector<T*> delete the contained Ts?]
>
> What exactly is it that is wrong here? I mean I could pick some minor
> nit like STL != C++ standardlibrary but that's probably not what you
> meant...


Bad English grammar parsing on my part. Nothing wrong with the C++ grammar
in any of the responses!

-cd


Carl Daniel [VC++ MVP]

2006-01-09, 11:10 pm

Carl Daniel [VC++ MVP] wrote:
> Mateusz Loskot wrote:
>
> I'd suggest that you read them as well.


Mateusz - my appologies. I misread your response as suggesting that
std::vector<MyClass*> would in fact delete the objects, which clearly it
doesn't, as your post correctly stated.

-cd


Stephen Howe

2006-01-09, 11:10 pm

> > No, you have to do it yourself. It applies to all STL containers.
>
> Wrong.


Your "Wrong" looks wrong. While the sentence above can be nitpicked, it is
essentially correct.
That is exactly what boost::shared_ptr does when the vector's destructor is
called if the contained shared_ptr's have been new'ed and push_back()'ed
into the container.

Stephen Howe


Serge Skorokhodov (216716244)

2006-01-09, 11:10 pm

Petar Popara wrote:
> Do I have in this case:
>
> vector<MyClass*> m_something; MyClass * mc = new MyClass();
> m_something.push_back(mc);
>
> to delete objects:
>
> for (unsigned int i=0; i<m_something.size(); i++) delete
> m_something[i];
>
> when m_something goes out of scope or vector template class
> will do it for me?


You may try pointer containers from boost instead of
boost::smart_ptr. It can be a better and more efficient solution.

On the other side, a container of smart_ptrs allows to safely
share the objects. If you need this functionality;)

--
Serge

Mateusz Łoskot

2006-01-09, 11:10 pm

Carl Daniel [VC++ MVP] wrote:
> Igor Tandetnik wrote:
>
>
>
> I was too. Mateusz' response was correct, but the first word of
> his response threw me off.


Ufff! I was really and I just started to doubt in all I've
read/learned till now.


>
> The OP was asking "do I need to delete the objects myself", to which Mateusz
> replied "No", then proceeded to explain why the answer is yes and how to get
> around it, just as I did in the other post.


Yes, you are right. My response was unprecise.
I took this as a question
"when m_something goes out of scope or vector template
class will do it for me? "
^^^^^^^^^^^^^^^^^^^

that's why I said "No" :-)))

Cheers

--
Mateusz Łoskot
http://mateusz.loskot.net
Mateusz Łoskot

2006-01-09, 11:10 pm

Carl Daniel [VC++ MVP] wrote:
> Carl Daniel [VC++ MVP] wrote:
>
>
>
> Mateusz - my appologies. I misread your response as suggesting that
> std::vector<MyClass*> would in fact delete the objects, which clearly it
> doesn't, as your post correctly stated.
>


No problem, as I posted above a minute ago.
I also understand my english is limited and sometimes used awkwardly.

Cheers

--
Mateusz Łoskot
http://mateusz.loskot.net
Stephen Howe

2006-01-09, 11:10 pm

> No problem, as I posted above a minute ago.
> I also understand my english is limited and sometimes used awkwardly.


Your english is fine.
I just wish I could speak fluent French, Italian, German, Spanish, Russian.
I always admire those speaking English where it is not their native tongue.

Stephen Howe



Mateusz Łoskot

2006-01-09, 11:10 pm

Stephen Howe wrote:
>
>
> Your english is fine.


Thanks

> I just wish I could speak fluent French, Italian, German, Spanish, Russian.
> I always admire those speaking English where it is not their
> native tongue.


It wish I could speak Russian and Spanish too. So, I wish us Good Luck!

Cheers
--
Mateusz Łoskot
http://mateusz.loskot.net
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com