STL

T

Tony Johansson

Hello experts!!

If I create object dynamically and store these pointers in a STL container
for example vector and when
this STL containers is destroyd the dynamically object will not be
destroyed.

What is the best solution to this kind of problem?

How is it possible to deallocate these object existing in a STL.

//Tony
 
T

Tobias Blomkvist

Tony Johansson sade:
Hello experts!!

If I create object dynamically and store these pointers in a STL container
for example vector and when
this STL containers is destroyd the dynamically object will not be
destroyed.

What is the best solution to this kind of problem?

Be aware of your design choices.
How is it possible to deallocate these object existing in a STL.

Iterate and delete.

Tobias
 
K

Kai-Uwe Bux

Greger said:
have alook at auto_ptr in the STL, see header memory in your stl imp.

Observe that the OP was asking specifically about storing pointers to
objects in an STL container, i.e., he is dealing with something like:

std::vector< Car * >

It is *not feasible* to replace this by

std::vector< std::auto_ptr< Car > >

since std::auto_ptr does not satisfy the requirements for a type to be used
in std::vector: assignable, copy constructible, ...


To the OP: very likely a reference counting smart pointer class will work
for you.


Best

Kai-Uwe Bux
 
G

Greger

Tony said:
Hello experts!!

If I create object dynamically and store these pointers in a STL container
for example vector and when
this STL containers is destroyd the dynamically object will not be
destroyed.

What is the best solution to this kind of problem?

How is it possible to deallocate these object existing in a STL.

//Tony
have alook at auto_ptr in the STL, see header memory in your stl imp.
 
P

peter.koch.larsen

Tony Johansson skrev:
Hello experts!!

If I create object dynamically and store these pointers in a STL container
for example vector and when
this STL containers is destroyd the dynamically object will not be
destroyed.

What is the best solution to this kind of problem?

How is it possible to deallocate these object existing in a STL.

//Tony

In general, don't use raw pointers in a STL-container. The auto_ptr
suggestion mentioned by someone else is also not feasible and should
not even compile. The reason for advising against raw pointers is
related to the fact that this will prevent you from using some of the
algorithms available on those containers. Everything that removes items
is more or less guaranteed to cause a memory-leak.

/Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top