auto_ptr vs shared_ptr

Discussion in 'C++' started by Markus Dehmann, Jun 1, 2005.

  1. The main difference between std::auto_ptr and boost::shared_ptr is that
    auto_ptr has the transfer-of-ownership semantic. That makes it impossible
    to store auto_ptrs in STL containers because the containers use the copy
    constructor when you store or get an element.

    So, is it recommended to use shared_ptr instead? Are there any
    disadvantages of shared_ptr? Is shared_ptr slower maybe?

    It seems for shared_ptr you only need the boost headers, not the libraries.
    Is that true?

    Thanks!
    Markus Dehmann, Jun 1, 2005
    #1
    1. Advertising

  2. Markus Dehmann

    Pete Becker Guest

    Markus Dehmann wrote:
    >
    > So, is it recommended to use shared_ptr instead? Are there any
    > disadvantages of shared_ptr? Is shared_ptr slower maybe?
    >


    shared_ptr allocates a control block from the heap.

    > It seems for shared_ptr you only need the boost headers, not the libraries.
    > Is that true?
    >


    Ask on the boost reflector.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jun 1, 2005
    #2
    1. Advertising

  3. Markus Dehmann

    Rapscallion Guest

    Markus Dehmann wrote:
    > So, is it recommended to use shared_ptr instead? Are there any
    > disadvantages of shared_ptr? Is shared_ptr slower maybe?


    Better don't waste your precious time and join the International Smart
    Pointer Haters Club, an ever growing conglomerate of people who think
    that smart pointers are not at all a smart idea. See e.g.
    http://groups-beta.google.com/group..._frm/thread/3ec52fd6f9d370c0/8a6742e2b0036832
    Rapscallion, Jun 1, 2005
    #3
  4. Markus Dehmann

    msalters Guest

    Markus Dehmann schreef:
    > The main difference between std::auto_ptr and boost::shared_ptr is that
    > auto_ptr has the transfer-of-ownership semantic. That makes it impossible
    > to store auto_ptrs in STL containers because the containers use the copy
    > constructor when you store or get an element.
    >
    > So, is it recommended to use shared_ptr instead? Are there any
    > disadvantages of shared_ptr? Is shared_ptr slower maybe?


    Disadvantages? Sure, the share count had to be stored somewhere.
    sizeof(auto_ptr<T>)==sizeof(T*) on many systems. Furthermore, this
    may decrease the cache efficiency.

    Furthermore, if the semantics you need /are/ transfer of ownership
    (source/sink functions) then std::auto_ptr makes it clear what happens.


    > It seems for shared_ptr you only need the boost headers, not the libraries.
    > Is that true?


    AFAIK, yes, but that's technically off-topic (and it could change).

    Regards,
    Michiel Salters
    msalters, Jun 2, 2005
    #4
    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. SerGioGio

    shared_ptr vs std::auto_ptr

    SerGioGio, Jul 3, 2003, in forum: C++
    Replies:
    3
    Views:
    5,428
    Alexander Terekhov
    Jul 3, 2003
  2. ctick
    Replies:
    4
    Views:
    8,942
    Richard Herring
    Jun 22, 2004
  3. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,529
    Dave Rahardja
    Jan 11, 2005
  4. Colin Caughie
    Replies:
    1
    Views:
    691
    Shooting
    Aug 29, 2006
  5. Jarek Blakarz

    passing auto_ptr as shared_ptr

    Jarek Blakarz, Dec 22, 2012, in forum: C++
    Replies:
    2
    Views:
    407
    Jarek Blakarz
    Dec 22, 2012
Loading...

Share This Page