473,387 Members | 1,897 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

boost::shared_ptr vs. auto_ptr

Are there any advantages of using boost::shared_ptr other than auto_ptr from
standard library?

Jul 22 '05 #1
5 2884
* ctick:
Are there any advantages of using boost::shared_ptr other than auto_ptr from
standard library?


There are a number of advantages.

First, they're two different beasts: std::auto_ptr transfers ownership
so that with some caution you can guarantee that only one pointer points
to a particular object at any time, whereas boost::shared_ptr provides
reference counting so that many pointers can point to the same object.

Second, you cannot put std::auto_ptr's in a standard container, but you
can with boost::shared_ptr.

Third, although the standard specially provides for calling a destructor
on an object of incomplete class, not all compilers support that. This
problem pops up in e.g. the pimpl idiom. With boost::shared_ptr you
replace the direct delete expression in std::auto_ptr with a custom
destroy-function that can be defined where the full definition of the
class is available, side-stepping the issue.

I could list up a fourth and fifth advantage, and perhaps more, but I
think that's enough. The main advantage of std::auto_ptr is that it's
always there and that it's standard. It should therefore be used when
the generality of e.g. boost::shared_ptr is not required.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2
On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, al***@start.no (Alf
P. Steinbach) wrote,
I could list up a fourth and fifth advantage, and perhaps more, but I
think that's enough. The main advantage of std::auto_ptr is that it's
always there and that it's standard. It should therefore be used when
the generality of e.g. boost::shared_ptr is not required.


I think that auto_ptr also has less overhead cost than shared_ptr's
ownership tracking mechanism when you are doing the simple things it is
capable of and have no need for shared ownership.

Jul 22 '05 #3
"ctick" <ct***@flare.com> wrote in message
news:g61Bc.6352$OB3.4344@bgtnsc05-
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?


You're comparing apples to oranges.
Jul 22 '05 #4
Siemel Naran wrote:

"ctick" <ct***@flare.com> wrote in message
news:g61Bc.6352$OB3.4344@bgtnsc05-
Are there any advantages of using boost::shared_ptr other than auto_ptr

from
standard library?


You're comparing apples to oranges.


http://www.people.virginia.edu/~rjh9u/apporang.html
Jul 22 '05 #5
In message <40****************@news.west.earthlink.net>, David Harmon
<so****@netcom.com.invalid> writes
On Sat, 19 Jun 2004 20:23:26 GMT in comp.lang.c++, al***@start.no (Alf
P. Steinbach) wrote,
I could list up a fourth and fifth advantage, and perhaps more, but I
think that's enough. The main advantage of std::auto_ptr is that it's
always there and that it's standard. It should therefore be used when
the generality of e.g. boost::shared_ptr is not required.


I think that auto_ptr also has less overhead cost than shared_ptr's
ownership tracking mechanism when you are doing the simple things it is
capable of and have no need for shared ownership.

For the _really_ simple things, consider boost::scoped_ptr. No
overheads, no sharing or transfer of ownership at all.

--
Richard Herring
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Philippe Guglielmetti | last post by:
I just ported old (VC6) working code to VC7.1 and have trouble with something like: class A; // forward typedef boost::smart_ptr<A> Aptr; class B{ Aptr a; virtual ~B(); // implemented...
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
2
by: adebaene | last post by:
Hello group, There seems to be a bug int the interop layer in VC2005 when dealing with certain pointer types (or values?) Here is a repro case using Boost version 1.32 and C++/CLI : using...
6
by: Toby Bradshaw | last post by:
Hi, Consider the following: class A { public: virtual bool foo() = 0; };
2
by: ketan | last post by:
Hi, While reading the Boost::shared_ptr implementation, I came across this method template<class Tclass shared_ptr { ..... template<class Y> shared_ptr(shared_ptr<Yconst & r,...
1
by: rssmps | last post by:
Hi, first post and here it goes... This is the current code (working) that I have. It works but for large number of A & B objects, it's not the most efficient. It's also not really heterogenous...
4
by: EnsGabe | last post by:
Suppose you have a class heirarchy as such: class Base{ .... }; class Mid1 : public Base{ ....
10
by: tradevol | last post by:
Hi, I am playing with boost pointer and try to wrap the following codes A* func(){ ... if(condition 1 ){ return a; } else
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.