Magic about auto_ptr !!

Discussion in 'C++' started by Binary, Oct 24, 2006.

  1. Binary

    Binary Guest

    Hi,

    I am reading a chinese book about STL, the book says below code will
    have no memory leak. I think its magic and wonder why the auto_ptr
    knows the memory should be freed when leaving the function call:

    void func()
    {
    auto_ptr<string> ps(new string("jjhou"));
    cout << *ps << endl;
    // leave the function without delte, the auto_ptr will release it
    automatically
    }

    TIA.
    ABAI
     
    Binary, Oct 24, 2006
    #1
    1. Advertising

  2. Binary

    flagos Guest

    You can find the answer here:

    http://www.parashift.com/c -faq-lite/dtors.html


    Binary wrote:
    > Hi,
    >
    > I am reading a chinese book about STL, the book says below code will
    > have no memory leak. I think its magic and wonder why the auto_ptr
    > knows the memory should be freed when leaving the function call:
    >
    > void func()
    > {
    > auto_ptr<string> ps(new string("jjhou"));
    > cout << *ps << endl;
    > // leave the function without delte, the auto_ptr will release it
    > automatically
    > }
    >
    > TIA.
    > ABAI
     
    flagos, Oct 24, 2006
    #2
    1. Advertising

  3. Binary

    benben Guest

    > I am reading a chinese book about STL, the book says below code will
    > have no memory leak. I think its magic and wonder why the auto_ptr
    > knows the memory should be freed when leaving the function call:
    >
    > void func()
    > {
    > auto_ptr<string> ps(new string("jjhou"));
    > cout << *ps << endl;
    > // leave the function without delte, the auto_ptr will release it
    > automatically
    > }


    A google on auto_ptr should give you tons of information. Basically, the
    auto_ptr class template is an example of how RAII idiom can help
    resource management.

    But before you get too excited please note that auto_ptr may not the be
    all and end all solution--far from it, in fact. Google on it, see what
    restrictions it has and what policy it imposes.

    That said, auto_ptr is a handy tool to use in the face of exception
    safety simply by the fact that it is properly destroyed (unlike a raw
    pointer) during stack unwinding. Again, google on it.

    Regards,
    Ben
     
    benben, Oct 24, 2006
    #3
  4. Binary

    Ye Dafeng Guest

    Binary wrote:
    > Hi,
    >
    > I am reading a chinese book about STL, the book says below code will
    > have no memory leak. I think its magic and wonder why the auto_ptr
    > knows the memory should be freed when leaving the function call:
    >
    > void func()
    > {
    > auto_ptr<string> ps(new string("jjhou"));
    > cout << *ps << endl;
    > // leave the function without delte, the auto_ptr will release it
    > automatically
    > }
    >
    > TIA.
    > ABAI
    >


    I suggest you read << C++ Primer >>, maybe you can find what you want.
     
    Ye Dafeng, Oct 24, 2006
    #4
  5. Binary wrote:
    > Hi,
    >
    > I am reading a chinese book about STL, the book says below code will
    > have no memory leak. I think its magic and wonder why the auto_ptr
    > knows the memory should be freed when leaving the function call:


    Because that is what auto_ptr's destructor does; it deletes the pointer
    that it is holding on to.

    --
    Clark S. Cox III
     
    Clark S. Cox III, Oct 24, 2006
    #5
    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,450
    Alexander Terekhov
    Jul 3, 2003
  2. alg

    auto_ptr

    alg, Jul 13, 2003, in forum: C++
    Replies:
    4
    Views:
    753
  3. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,560
    Dave Rahardja
    Jan 11, 2005
  4. Sousuke
    Replies:
    9
    Views:
    1,157
    Bart van Ingen Schenau
    Mar 16, 2010
  5. Giles Bowkett
    Replies:
    9
    Views:
    414
    Giles Bowkett
    Dec 17, 2007
Loading...

Share This Page