473,396 Members | 1,804 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,396 software developers and data experts.

Tell me again what's wrong with intrusive pointers?

It's my understanding that intrusive pointers are frowned upon. For example
this is from the boost::intrusive_ptr documentation: "As a general rule, if
it isn't obvious whether intrusive_ptr better fits your needs than
shared_ptr, try a shared_ptr-based design first."

This tells me that intrusive pointers are faster and leaner than any of the
other alternative other than raw pointers.
http://www.boost.org/libs/smart_ptr/smarttests.htm

To my way of thinking they are easier to use when working with recursive
data structures (trees). I was trying to use boost::shared_ptr because of
the comment quoted above, and similar comments I've read elsewhere. It
often makes sense when constructing nodes to pass the parent as a
parameter, and add `this' to the parent as a child. I tried doing that
with a shared_ptr implementation using boost::enable_shared_from_this.
When I executed the code, it threw a bad_weak_ptr exception, which traced
back to where I was trying to pass a shared_ptr to `this'. I assume the
problem is that I cannot use that feature until the object is fully
constructed and an owner is established.

If the argument against intrusive pointers has to do with not wanting to
mess with the internals of the class being pointed to, that seems obviated
by the use of boost::enable_shared_from_this. AFAIK, using shared_ptr it
is possible to establish two independent reference counts which is a
guaranteed path to disaster. That can't happen with intrusive pointers.
What are the reasons for favoring shared_ptr over intrusive_ptr?
--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 30 '06 #1
2 2896
* Steven T. Hatton:
What are the reasons for favoring shared_ptr over intrusive_ptr?
One reason is that intrusive reference counting (I don't know about the
Boost version) in general makes it possible to get away with using raw
pointers in client code. And that temptation is /very/ very strong:
it's the micro-optimization attractor, which for programmers is stronger
than the sex drive, even stronger than death-avoidance. Another reason
is that it necessitates using at least two kinds of smart pointers for
essentially the same conceptual-level purpose: non-intrusive smart
pointers for those classes that can't be outfitted with internal
reference counting, and intrusive ones, and for this, more is less.

--
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?
Nov 30 '06 #2
Alf P. Steinbach wrote:
* Steven T. Hatton:
>What are the reasons for favoring shared_ptr over intrusive_ptr?

One reason is that intrusive reference counting (I don't know about the
Boost version) in general makes it possible to get away with using raw
pointers in client code.
With boost::shared_ptr you can do that, so in this case that doesn't apply.
And that temptation is /very/ very strong:
it's the micro-optimization attractor, which for programmers is stronger
than the sex drive, even stronger than death-avoidance. Another reason
is that it necessitates using at least two kinds of smart pointers for
essentially the same conceptual-level purpose: non-intrusive smart
pointers for those classes that can't be outfitted with internal
reference counting, and intrusive ones, and for this, more is less.
With OpenSceneGraph there is a galactic base class (like a universal base
class, but not as inclusive) called osg::Referenced. If you want to use
the corresponding osg::ref_ptr, you simply derive from referenced and have
at it. There are things to be aware of such as the consequences of
deriving from osg::Referenced and not making the destructor protected, and
then putting an instance on the stack.

The problem of introducing two variants of smart pointers doesn't seem very
big in this situation. There are two kinds of things I'd be keeping track
of using the intrusive reference counting, nodes and node data. I probably
won't need shared pointers beyond those.

I can probably make the shared_ptr work if I forego trying to append the new
node to its parent in the constructor. I probably should learn to work
with it, if for no other reason, because other people seem to think it's
the way to go.

--
NOUN:1. Money or property bequeathed to another by will. 2. Something handed
down from an ancestor or a predecessor or from the past: a legacy of
religious freedom. ETYMOLOGY: MidE legacie, office of a deputy, from OF,
from ML legatia, from L legare, to depute, bequeath. www.bartleby.com/61/
Nov 30 '06 #3

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

Similar topics

3
by: Jonathan | last post by:
Rather than me explain the problem, read this exchange of emails between my web host and myself, then tell me what I can tell them! I had a problem with their admin site all day, and after me...
303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
0
by: Sabyasachi Basu | last post by:
Most C++ object serialization techniques are intrusive, that is they require all serializable classes need to have functions to stream in and stream out their contents. I came across...
9
by: Marcin Kalicinski | last post by:
Hi everybody, I am creating an intrusive list ('next' and 'prev' pointers are stored within an object that is in a list). One method of doing that is to inherit all objects from some class that...
6
by: Pradeep | last post by:
Hi, I am using intrusive pointers for my code but i am not much aquianted with them. I am using an example to illustrate my problem as i can't share the code. I have two clases ( say A and B)...
1
by: Pradeep | last post by:
Hi, I am working with Intrusive pointers in a DLL and I need to set the value of the intrusive pointer to an object to NULL from inside the method of that class. So here's the scenario. I...
156
by: Dennis | last post by:
Ok, I'm trying to dispose of every object that I create that has a dispose method based on advice from this newsgroup. However, I'm not sure how to dispose of the following object that was created...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
6
by: stork | last post by:
C++ is always evolving. Within C++, there tend to be two communities. There is the community pushing for performance features, getting ever closer to the metal, and then, there is the...
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.