What does auto_ptr::reset mean?

This is a discussion on What does auto_ptr::reset mean? within the C++ Programming forums, part of the General Programming Boards category; According to reference, the member function evaluates the expression delete myptr, but only if the stored pointer value myptr changes ...

  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
    21,758
    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.
    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
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    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

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