-
August 8th, 2008, 06:07 PM
#1
shared_ptr VS. intrusive_ptr?
What I understand, intrusive_ptr provides better performnce than shared_ptr, but requires the type being refered to provide its own counting mechanism. I can understand that intrusive_ptr is fast and works good, if the object itself can provide that. But how does shared_ptr work? How does it know how many references there is to the same object? Or when deleted, when to delete the object being refered to? If the object being refered to cannot store the number of references, where is it sored?
-
August 8th, 2008, 06:28 PM
#2
Re: shared_ptr VS. intrusive_ptr?
The reference count is stored in the shared_ptr object. You can look at the source code http://www.boost.org/doc/libs/1_35_0...shared_ptr.hpp
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
August 8th, 2008, 06:45 PM
#3
Re: shared_ptr VS. intrusive_ptr?
I think I understand your real question.
if I do the following then a crash will result
Code:
class foo;
foo* f = new foo;
shared_ptr ptr_1(f);
shared_ptr ptr_2(f);
with shared pointer, multiple shared pointers must come from an original shared_ptr
the following code is ok
Code:
class foo;
foo* f = new foo;
shared_ptr ptr_1(f);
shared_ptr ptr_2 = ptr_1;
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
August 9th, 2008, 12:37 AM
#4
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by TriKri
where is it sored?
In a counter allocated on the heap.
That's the reason why intrusive smart pointers are faster. They don't allocate a counter. It's supplied by the object.
-
August 9th, 2008, 04:35 AM
#5
Re: shared_ptr VS. intrusive_ptr?
What's the heap? Can the program allocate from the heap?
-
August 9th, 2008, 05:03 AM
#6
Re: shared_ptr VS. intrusive_ptr?
You're asking about pointers and you don't know what the heap is? It's where the object that you're pointing to is allocated.
Incidentally, don't get hung up on "efficiency". I'll lay good odds that you couldn't tell the difference between the two in a real situation. The difference between them has been alluded to by souldog - intrusive reference pointers to a single object can be created independently of each other, whereas shared pointers to a single object all have to be created by copying from a single original.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
-
August 9th, 2008, 03:59 PM
#7
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by TriKri
What's the heap? Can the program allocate from the heap?
The heap is a memory where objects you allocate using the new keyword are stored.
I suggest you start by using the shared smart pointer, either the one supplied by Boost or the one from any TR1 implementation. The latter is a candidate for being included in the C++ standard library. Note that no intrusive smart pointer is suggested for inclusion. It's generally best sticking to the standard, unless you're an expert knowing exactly what you want.
Last edited by _uj; August 9th, 2008 at 04:02 PM.
-
August 10th, 2008, 12:55 AM
#8
Re: shared_ptr VS. intrusive_ptr?
I thought the shared_ptr being included in TR1 is just the boost shared_ptr.
Am I wrong about that?
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
August 13th, 2008, 02:05 AM
#9
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by souldog
I thought the shared_ptr being included in TR1 is just the boost shared_ptr.
Am I wrong about that?
Features aren't just included as is. For example the Boost shared_ptr makes some promises about its multithreading behavior. I doubt the C++ standard will maintain that.
-
August 13th, 2008, 05:14 AM
#10
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by _uj
Features aren't just included as is. For example the Boost shared_ptr makes some promises about its multithreading behavior. I doubt the C++ standard will maintain that.
Multithreading behavior? Now I'm lost...
-
August 13th, 2008, 09:40 AM
#11
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by TriKri
Multithreading behavior? Now I'm lost...
http://www.boost.org/doc/libs/1_35_0...m#ThreadSafety
-
August 14th, 2008, 02:51 AM
#12
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by _uj
Features aren't just included as is. For example the Boost shared_ptr makes some promises about its multithreading behavior. I doubt the C++ standard will maintain that.
I don't see how the C++ standard would not be able to maintain the multithreading behavior. Isn't that pretty basic?
-
August 14th, 2008, 02:55 AM
#13
Re: shared_ptr VS. intrusive_ptr?
I don't see how the C++ standard would not be able to maintain the multithreading behavior. Isn't that pretty basic?
I think that what _uj meant is the next edition of the C++ Standard might not guarantee such behaviour.
-
August 14th, 2008, 03:09 AM
#14
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by TriKri
I don't see how the C++ standard would not be able to maintain the multithreading behavior. Isn't that pretty basic?
Threading is one of those features which is too OS dependent.
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
August 14th, 2008, 04:44 AM
#15
Re: shared_ptr VS. intrusive_ptr?
Originally Posted by laserlight
I think that what _uj meant is the next edition of the C++ Standard might not guarantee such behaviour.
I know, but I mean, what could go wrong? How do you construct a class which does not guarantee the behavior? I can't imagine any.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|