Connecting Tech Pros Worldwide Help | Site Map

STL

 
LinkBack Thread Tools Search this Thread
  #1  
Old August 20th, 2005, 02:15 PM
Tony Johansson
Guest
 
Posts: n/a
Default STL

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, 02:25 PM
Srini
Guest
 
Posts: n/a
Default Re: STL

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

Srini

  #3  
Old August 20th, 2005, 04:05 PM
Tobias Blomkvist
Guest
 
Posts: n/a
Default 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, 08:25 AM
Greger
Guest
 
Posts: n/a
Default 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, 09:05 AM
Kai-Uwe Bux
Guest
 
Posts: n/a
Default 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, 12:45 PM
peter.koch.larsen@gmail.com
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.