Thread: What does auto_ptr::reset mean?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    What does auto_ptr::reset mean?

    According to reference, the member function evaluates the expression delete myptr, but only if the stored pointer value myptr changes as a result of function call. It then replaces the stored pointer with ptr.

    But I still don't get it. Does it mean this member function deletes current myptr and points to another memory space?

    Say
    Code:
    std::auto_ptr<int> p (new int(100));
    p.reset(new int(200));
    Will the memory of new int(100) be freed after that?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Does it mean this member function deletes current myptr and points to another memory space?
    Yes, assuming by myptr you mean the pointer pointing to the int with the value of 100.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Code:
    int* my100ptr = new int(100);
    int* my200ptr = new int(200);
    
    std::auto_ptr<int> ap(my100ptr);
    
    ap.reset(my100ptr); // delete is not called.
    
    ap.reset(my200ptr); // delete is called on my100ptr.

Popular pages Recent additions subscribe to a feed