auto_ptr vs shared_ptr

M

Markus Dehmann

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!
 
P

Pete Becker

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

shared_ptr allocates a control block from the heap.
It seems for shared_ptr you only need the boost headers, not the libraries.
Is that true?

Ask on the boost reflector.
 
M

msalters

Markus Dehmann schreef:
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?

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.

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

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

Regards,
Michiel Salters
 

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,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top