when to use auto_ptr

This is a discussion on when to use auto_ptr within the C++ Programming forums, part of the General Programming Boards category; Starting to learn about some of what the STL has to offer, I came across the smart pointer template auto_ptr ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    242

    when to use auto_ptr

    Starting to learn about some of what the STL has to offer, I came across the smart pointer template auto_ptr and wondered when one wants to use or not use it instead of a regular pointer.

    Would most of you here just routinely use auto_ptr when you plan on dynamically allocating memory?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Aisthesis
    Would most of you here just routinely use auto_ptr when you plan on dynamically allocating memory?
    Not really, since one would routinely need containers, and auto_ptr has unusual copy semantics, yet one might need more normal copy semantics and shared ownership, in which case std::tr1::shared_ptr would be more appropriate.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Use it in places where you always want to delete the pointer as soon as the auto_ptr goes out of scope; and only if it's a pointer to a single object instead of an array of objects; and only if you don't plan to store the pointer in an STL container...

    Basically, auto_ptr was a good attempt to create a smart pointer, but it has too many limitations compared with the Boost smart pointers. I'd recommend using boost::smart_ptr which can be safely stored in STL containers and can point to arrays of objects and is reference counted...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by cpjust
    Use it in places where you always want to delete the pointer as soon as the auto_ptr goes out of scope; and only if it's a pointer to a single object instead of an array of objects; and only if you don't plan to store the pointer in an STL container...
    Yeah, this is probably one of my main uses for auto_ptr. boost::scoped_ptr is more appropriate for this, but Boost might not be available. (Admittedly, a TR1 implementation might not be available either, but if Boost is available, then you should be able to use Boost.TR1, so I reason that relying on a TR1 implementation being available is a better bet than directly relying on Boost being available.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    smart pointer template auto_ptr
    Auto_ptr is not a true 'smart' pointer. Be careful how and when you use it.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    2,032
    I think the best use of auto_ptr is as a function parameter that takes ownership of the pointer passed. A regular pointer does not imply passing ownership, and it is generally assumed that it doesn't. Also a regular pointer will not be set to null in the original function automatically, as it rightly should be when the function cannot know if the pointer is valid anymore.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    242
    Thanks! That gives me a good general idea of some of the available options. It will probably be a while until I start exploring boost and tr1, but nice to know a little bit about what they offer.

    Is tr1 by any chance included in MS Visual Studio 2008? Or does one need to get it elsewhere?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    Quote Originally Posted by Aisthesis
    Is tr1 by any chance included in MS Visual Studio 2008? Or does one need to get it elsewhere?
    Try installing the Visual C++ 2008 Feature Pack Release.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21