Connecting Tech Pros Worldwide Help | Site Map

auto_ptr vs shared_ptr

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 04:58 AM
Markus Dehmann
Guest
 
Posts: n/a
Default auto_ptr vs shared_ptr

The main difference between std::auto_ptr and boost::shared_ptr is that
auto_ptr has the transfer-of-ownership semantic. That makes it impossible
to store auto_ptrs in STL containers because the containers use the copy
constructor when you store or get an element.

So, is it recommended to use shared_ptr instead? Are there any
disadvantages of shared_ptr? Is shared_ptr slower maybe?

It seems for shared_ptr you only need the boost headers, not the libraries.
Is that true?

Thanks!



  #2  
Old July 23rd, 2005, 04:58 AM
Pete Becker
Guest
 
Posts: n/a
Default Re: auto_ptr vs shared_ptr

Markus Dehmann wrote:[color=blue]
>
> So, is it recommended to use shared_ptr instead? Are there any
> disadvantages of shared_ptr? Is shared_ptr slower maybe?
>[/color]

shared_ptr allocates a control block from the heap.
[color=blue]
> It seems for shared_ptr you only need the boost headers, not the libraries.
> Is that true?
>[/color]

Ask on the boost reflector.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
  #3  
Old July 23rd, 2005, 04:58 AM
Rapscallion
Guest
 
Posts: n/a
Default Re: auto_ptr vs shared_ptr

Markus Dehmann wrote:[color=blue]
> So, is it recommended to use shared_ptr instead? Are there any
> disadvantages of shared_ptr? Is shared_ptr slower maybe?[/color]

Better don't waste your precious time and join the International Smart
Pointer Haters Club, an ever growing conglomerate of people who think
that smart pointers are not at all a smart idea. See e.g.
http://groups-beta.google.com/group/...6742e2b0036832

  #4  
Old July 23rd, 2005, 04:58 AM
msalters
Guest
 
Posts: n/a
Default Re: auto_ptr vs shared_ptr



Markus Dehmann schreef:[color=blue]
> The main difference between std::auto_ptr and boost::shared_ptr is that
> auto_ptr has the transfer-of-ownership semantic. That makes it impossible
> to store auto_ptrs in STL containers because the containers use the copy
> constructor when you store or get an element.
>
> So, is it recommended to use shared_ptr instead? Are there any
> disadvantages of shared_ptr? Is shared_ptr slower maybe?[/color]

Disadvantages? Sure, the share count had to be stored somewhere.
sizeof(auto_ptr<T>)==sizeof(T*) on many systems. Furthermore, this
may decrease the cache efficiency.

Furthermore, if the semantics you need /are/ transfer of ownership
(source/sink functions) then std::auto_ptr makes it clear what happens.

[color=blue]
> It seems for shared_ptr you only need the boost headers, not the libraries.
> Is that true?[/color]

AFAIK, yes, but that's technically off-topic (and it could change).

Regards,
Michiel Salters

 

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,662 network members.