Auto_ptr

This is a discussion on Auto_ptr within the C++ Programming forums, part of the General Programming Boards category; What is Auto_ptr? Is it better than normal pointer assignment? If so, I would use this forever. Code: void processAdoptions(istream& ...

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    14

    Auto_ptr

    What is Auto_ptr? Is it better than normal pointer assignment? If so, I would use this forever.
    Code:
    void processAdoptions(istream& dataSource)
    {
      while (dataSource) {
        auto_ptr<ALA> pa(readALA(dataSource));
        pa->processAdoption();
      }
    }
    
    ----------------------------------------------------------------------
    void processAdoptions(istream& dataSource)
    {
      while (dataSource) {
        ALA *pa = readALA(dataSource);
      try {
          pa->processAdoption();
      }
      catch (...) {              // catch all exceptions
        delete pa;               // avoid resource leak when an
                                 // exception is thrown
        throw;                   // propagate exception to caller
      }
      delete pa;                 // avoid resource leak when no
     }                           // exception is thrown
    }
    ---------------------------------------------------------------------

  2. #2
    Registered User
    Join Date
    Sep 2009
    Posts
    26

    Smile

    auto_ptr is is a smart pointer.It works smartly as if u declare this pointer and allocate memory dynamically for it then there is no need to deallocate memory for it using free() or delete, becoz its automaticaly cleaned up.

    Code:
    auto_ptr<ALA> pa(readALA(dataSource));
    pa is a smart pointer so there is no need to de-alloacate memory for it as u have done below...

    Code:
    try {
          pa->processAdoption();
      }
      catch (...) {              // catch all exceptions
        delete pa;               // avoid resource leak when an
                                 // exception is thrown
        throw;                   // propagate exception to caller
      }
      delete pa;                 // avoid resource leak when no
     }                           // exception is thrown
    }

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Your example appears to be from a text (Effective C++) discussing the benefits of automating memory management. auto_ptr is fine for described usage.

    You can also read about auto_ptr here.

    There are certain pitfalls (in connection with unusual copy semantics). With C++0x a more robust smart pointer called scoped_ptr will be added (it will be uncopyable but movable, which means you'll be able to store them in containers).

    Another type of widely used smart pointer is shared_ptr (also to be added to the standard library) which allows you to have multiple smart pointers to the same resource and the last one to go out of scope does the deallocation.

    Look up boost's smart pointers as well.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,888
    Quote Originally Posted by anon
    With C++0x a more robust smart pointer called scoped_ptr will be added (it will be uncopyable but movable, which means you'll be able to store them in containers).
    That does not sound right. Since it is noncopyable, a scoped_ptr cannot be stored in a container. Furthermore, I think that you are talking about unique_ptr, which would be the replacement for auto_ptr in C++0x, but could be stored in containers unlike auto_ptr. scoped_ptr does not appear to be a likely candidate to be added to C++0x at the moment, considering that it is not in the current working draft.
    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
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, I mixed up the names. unique_ptr, as far as I know, will have a move constructor and move assignment operator, and as far as containers add support for rvalue references, storing unique_ptr's will be possible.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    scoped_ptr won't be in C++0x, and Boost's version won't be movable.
    unique_ptr is the new C++0x smart pointer. It's not copyable, but movable.
    C++0x STL containers will be able to store move-only objects such as unique_ptrs.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Just remember not to store a pointer to an array of objects in an auto_ptr because the auto_ptr destructor uses delete ptr; instead of delete [] ptr; which could lead to a memory leak.
    "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

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