Connecting Tech Pros Worldwide Help | Site Map

auto_ptr vs shared_ptr

  #1  
Old July 23rd, 2005, 05:58 AM
Markus Dehmann
Guest
 
Posts: n/a
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, 05:58 AM
Pete Becker
Guest
 
Posts: n/a

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, 05:58 AM
Rapscallion
Guest
 
Posts: n/a

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, 05:58 AM
msalters
Guest
 
Posts: n/a

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

Closed Thread


Similar Threads
Thread Thread Starter Forum Replies Last Post
heap vs. stack Brian Buderman answers 9 December 18th, 2006 05:55 PM
API design choice pointer vs. object Divick answers 17 March 18th, 2006 01:05 AM
boost::shared_ptr vs. auto_ptr ctick answers 5 July 22nd, 2005 01:59 PM
shared_ptr vs std::auto_ptr SerGioGio answers 3 July 19th, 2005 03:55 PM