CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Jul 2007
    Posts
    17

    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?

  2. #2
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  4. #4
    Join Date
    Nov 2003
    Posts
    1,405

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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.

  5. #5
    Join Date
    Jul 2007
    Posts
    17

    Re: shared_ptr VS. intrusive_ptr?

    What's the heap? Can the program allocate from the heap?

  6. #6
    Join Date
    Apr 1999
    Location
    Altrincham, England
    Posts
    4,470

    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


  7. #7
    Join Date
    Nov 2003
    Posts
    1,405

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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.

  8. #8
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    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."

  9. #9
    Join Date
    Nov 2003
    Posts
    1,405

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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.

  10. #10
    Join Date
    Jul 2007
    Posts
    17

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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...

  11. #11
    Join Date
    Nov 2003
    Posts
    1,405

    Re: shared_ptr VS. intrusive_ptr?

    Quote Originally Posted by TriKri
    Multithreading behavior? Now I'm lost...
    http://www.boost.org/doc/libs/1_35_0...m#ThreadSafety

  12. #12
    Join Date
    Jul 2007
    Posts
    17

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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?

  13. #13
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  14. #14
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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."

  15. #15
    Join Date
    Jul 2007
    Posts
    17

    Re: shared_ptr VS. intrusive_ptr?

    Quote 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.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured