Is it possible to "delete" auto_ptr or scoped_ptr?

Discussion in 'C++' started by A, Oct 25, 2010.

  1. A

    A Guest

    I know that I can delete this:

    TStruct *a = new TStruct();
    delete a;

    but is it possible to delete this too:

    std::auto_ptr<TStruct> a(new TStruct());

    or this

    boost::scoped_ptr<TStruct> a(new TStruct());

    Basically, I'd like to use the advantages of auto_ptr or scoped_ptr but with
    ability to remove them from memory when they are not needed but function
    didn't terminate yet.

    I see there is a reset() - is that what I need?

    in other words, how do i call auto_ptr or scoped_ptr destructor?
    A, Oct 25, 2010
    #1
    1. Advertising

  2. A

    A Guest

    I think a.reset() does what I want (releases pointer and frees memory for
    both auto_ptr and boost::scoped_ptr) but I'd just like to confirm this here.
    A, Oct 26, 2010
    #2
    1. Advertising

  3. A wrote:
    > but is it possible to delete this too:
    >
    > std::auto_ptr<TStruct> a(new TStruct());
    >
    > or this
    >
    > boost::scoped_ptr<TStruct> a(new TStruct());


    Of course! Simply assign NULL to either of them. It will do what you want.


    > I see there is a reset() - is that what I need?


    Yes. This is equivalent to assigning NULL.


    > in other words, how do i call auto_ptr or scoped_ptr destructor?


    You should never ever call the destuctor directly. Setting them to NULL
    is not the same since they might point to other objects later.


    Marcel
    Marcel Müller, Oct 26, 2010
    #3
  4. A

    Guest

    On Oct 25, 4:43 pm, "A" <> wrote:
    > I know that I can delete this:
    >
    > TStruct *a = new TStruct();
    > delete a;
    >
    > but is it possible to delete this too:
    >
    > std::auto_ptr<TStruct> a(new TStruct());
    >
    > I see there is a reset() - is that what I need?



    I don't know about scoped_ptr, but with std::auto_ptr you just need
    to give it another pointer with ::reset(), and the memory it currently
    points to will be deleted (freed).

    NULL is a valid value to pass ::reset() (which is what it defaults
    to anyway), so you can just use:

    a.reset(); // frees/deleted what a was pointing to

    Now the a variable will be pointing to NULL, so be careful about de-
    referencing it, or else your program will likely crash.

    I hope this helps.

    -- Jean-Luc
    , Oct 26, 2010
    #4
  5. A

    A Guest

    > I don't know about scoped_ptr, but with std::auto_ptr you just need
    >to give it another pointer with ::reset(), and the memory it currently
    >points to will be deleted (freed).


    scoped_ptr is almost identical to auto_ptr except it is non-copyable by
    default. it also has reset(), probably the NULL trick would do work as well.
    A, Oct 26, 2010
    #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. Siemel Naran

    auto_ptr<Derived> to auto_ptr<Base>

    Siemel Naran, Jan 10, 2005, in forum: C++
    Replies:
    2
    Views:
    1,552
    Dave Rahardja
    Jan 11, 2005
  2. Replies:
    3
    Views:
    741
    Joe Gottman
    Jan 21, 2006
  3. kathy
    Replies:
    2
    Views:
    349
    Jeff Flinn
    Apr 12, 2006
  4. none
    Replies:
    6
    Views:
    1,051
    Dizzy
    Jul 11, 2007
  5. Sousuke
    Replies:
    9
    Views:
    1,144
    Bart van Ingen Schenau
    Mar 16, 2010
Loading...

Share This Page