Reference Counted Smart Pointers And STL Containers

Discussion in 'C++' started by Matthias Kaeppler, Aug 27, 2005.

  1. Hi,

    I was wondering, since STL containers are based around copying, whether
    it's a good idea to use reference counted smart pointers, such as
    boost::shared_ptr in STL containers.
    I can't store the objects directly in a container, because they must
    not be duplicated, so I have to use pointers. I'm just not certain about
    using raw pointers or some kind of smart pointer.

    Regards,
    Matthias
     
    Matthias Kaeppler, Aug 27, 2005
    #1
    1. Advertising

  2. Matthias Kaeppler

    Guest

    Matthias Kaeppler skrev:

    > Hi,
    >
    > I was wondering, since STL containers are based around copying, whether
    > it's a good idea to use reference counted smart pointers, such as
    > boost::shared_ptr in STL containers.
    > I can't store the objects directly in a container, because they must
    > not be duplicated, so I have to use pointers. I'm just not certain about
    > using raw pointers or some kind of smart pointer.
    >
    > Regards,
    > Matthias


    A reference-counted pointer such as boost::shared_ptr is almost always
    the correct choice in situations such as this.

    /Peter
     
    , Aug 27, 2005
    #2
    1. Advertising

  3. wrote:
    > A reference-counted pointer such as boost::shared_ptr is almost always
    > the correct choice in situations such as this.


    Hm, yes probably you're right. It's just that I'm getting segfaults on
    program exit, and it's almost certainly due to the smart pointers I'm
    holding in a std::set. I'm not sure what I'm doing wrong.
    Is there anything special I have to watch out for when storing a
    boost::shared_ptr in an STL container?

    Regards,
    Matthias
     
    Matthias Kaeppler, Aug 27, 2005
    #3
  4. Matthias Kaeppler

    Kai-Uwe Bux Guest

    Matthias Kaeppler wrote:

    > wrote:
    >> A reference-counted pointer such as boost::shared_ptr is almost always
    >> the correct choice in situations such as this.

    >
    > Hm, yes probably you're right. It's just that I'm getting segfaults on
    > program exit, and it's almost certainly due to the smart pointers I'm
    > holding in a std::set. I'm not sure what I'm doing wrong.
    > Is there anything special I have to watch out for when storing a
    > boost::shared_ptr in an STL container?
    >
    > Regards,
    > Matthias


    Could you post a (small if possible) piece of code that demonstrates the
    problem?


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Aug 27, 2005
    #4
  5. Kai-Uwe Bux wrote:
    > Could you post a (small if possible) piece of code that demonstrates the
    > problem?


    Sure. I have started a new thread dealing with the problem directly, I
    will post the relevant parts of my code there.
     
    Matthias Kaeppler, Aug 27, 2005
    #5
  6. Matthias Kaeppler

    Axter Guest

    Matthias Kaeppler wrote:
    > Hi,
    >
    > I was wondering, since STL containers are based around copying, whether
    > it's a good idea to use reference counted smart pointers, such as
    > boost::shared_ptr in STL containers.
    > I can't store the objects directly in a container, because they must
    > not be duplicated, so I have to use pointers. I'm just not certain about
    > using raw pointers or some kind of smart pointer.
    >
    > Regards,
    > Matthias


    If you want to make sure you don't have duplicates, consider using
    either a clone smart pointer or a COW smart pointer.
    See following links:
    http://code.axter.com/copy_ptr.h
    http://code.axter.com/cow_ptr.h
    http://code.axter.com/clone_ptr.h

    Both the copy_ptr and clone_ptr class will make sure you only have
    unique pointers, and have strict pointer ownership logic, which means
    they do NOT share their pointers.

    cow_ptr does share the pointer, until an attempt is made to access the
    non-constant version of the pointer. At that point, it will clone
    (copy) itself.
     
    Axter, Sep 10, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    4
    Views:
    332
    tom_usenet
    Feb 19, 2004
  2. JackC
    Replies:
    3
    Views:
    599
    Alan Griffiths
    Aug 13, 2004
  3. MotoK
    Replies:
    59
    Views:
    1,848
    Keith Thompson
    Sep 15, 2006
  4. ma740988
    Replies:
    0
    Views:
    313
    ma740988
    Dec 12, 2005
  5. Angus
    Replies:
    1
    Views:
    291
    Maxim Yegorushkin
    Nov 19, 2008
Loading...

Share This Page