Connecting Tech Pros Worldwide Help | Site Map

STL

  #1  
Old August 20th, 2005, 03:15 PM
Tony Johansson
Guest
 
Posts: n/a
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


  #2  
Old August 20th, 2005, 03:25 PM
Srini
Guest
 
Posts: n/a

re: STL


Use smart pointers... Boost's shared_ptr would be a good choice I
guess.

Srini

  #3  
Old August 20th, 2005, 05:05 PM
Tobias Blomkvist
Guest
 
Posts: n/a

re: STL


Tony Johansson sade:[color=blue]
> 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?[/color]

Be aware of your design choices.
[color=blue]
>
> How is it possible to deallocate these object existing in a STL.
>[/color]

Iterate and delete.

Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
  #4  
Old August 21st, 2005, 09:25 AM
Greger
Guest
 
Posts: n/a

re: STL


Tony Johansson wrote:
[color=blue]
> 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[/color]
have alook at auto_ptr in the STL, see header memory in your stl imp.
--
http://www.gregerhaga.net
  #5  
Old August 21st, 2005, 10:05 AM
Kai-Uwe Bux
Guest
 
Posts: n/a

re: STL


Greger wrote:
[color=blue]
> Tony Johansson wrote:
>[color=green]
>> 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[/color]
> have alook at auto_ptr in the STL, see header memory in your stl imp.[/color]

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
  #6  
Old August 21st, 2005, 01:45 PM
peter.koch.larsen@gmail.com
Guest
 
Posts: n/a

re: STL



Tony Johansson skrev:
[color=blue]
> 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[/color]

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

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
STL.NET news Andrew Roberts answers 20 February 15th, 2006 12:55 PM
Compile error with SGI STL rajd99 answers 0 September 16th, 2005 08:54 PM
STL Standards Document RR answers 11 July 23rd, 2005 05:17 AM
STL Functionality Steve answers 2 July 22nd, 2005 10:43 AM
To STL or not to STL Allan Bruce answers 41 July 19th, 2005 08:20 PM