TITLE: what is auto_ptr<> (Newsgroups: comp.lang.c++.moderated, 22 Jan 97) PASSY: A. Marc Passy > > I've seen talk of something called an auto_ptr<>. Details, please. > Please include any documented references if you know of any. LIBERTY: Jesse Liberty Auto pointers are parameterized (template) objects which hold a pointer but which live on the stack. That is, inside the autopointer is a real pointer which in turn points to an object on the heap. They were created because of this problem: you create an object on the heap and get back a pointer. You use the object and then you delete it. However, if before you delete it you throw an exception, you'll exit the function and never delete the object. When you throw an exception local objects (on the stack) are destroyed, but objects on the heap are NOT destroyed. By putting your pointer inside an autopointer, and then putting that autopointer on the stack, the autopointer WILL be destroyed when an excception is thrown. In its destructor it will destroy the object on the heap, thus plugging the memory leak. The trick with autopointers is that they are neither automatic nor really pointers. You can use them as pointers IN MANY WAYS, but not in every way. Thus, you want to be VERY careful to understand what happens when you pass an autopointer by value or you make a copy. Try this code, if it compiles, try uncommenting out the call by value and the copy -- see what happens. The trick is that autopointers RELEASE their hold on the pointer when they are copied. That is why you pass by reference, to avoid creating a copy. // *********************************************** // FILE: Autoptr.cpp // // PURPOSE Illustrates use of the autoptr. // // NOTES: // // COPYRIGHT: Copyright (C) 1996 Liberty Associates, Inc. // All Rights Reserved // // THIS SOFTWARE WAS CREATED FOR ILLUSTRATION PURPOSES ONLY. // LIBERTY ASSOCIATES, INC. AND ITS EMPLOYEES AND OFFICERS // MAKE NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY // OF THIS SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT // NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS // FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT. LIBERTY ASSOCIATES, INC // SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A // RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES. // // *********************************************** #include #include class Cat { public: Cat (int age):itsAge(age) {cout << "cat constructor\n";} ~Cat () {cout << "cat destructor\n";} void SetAge (int age) { itsAge = age; } int GetAge ()const { return itsAge; } private: int itsAge; }; class Employee { public: Employee (int age, int yrsSvc); ~Employee () {cout << "employee destroyed\n";} private: int myAge; int myYrsSvc; }; Employee::Employee(int age, int yrs): myAge(age), myYrsSvc(yrs) {} void ReferenceFunction(int age, auto_ptr& rhs) { rhs->SetAge(age); cout << "In ReferenceFunction. Cat is " << rhs->GetAge() << " years old\n"; // auto_ptr localPtr; //localPtr = rhs; //localPtr->SetAge(22); //cout << "In ReferenceFunction. localPtr's cat is " << localPtr->GetAge() << " years old\n"; //cout << "In ReferenceFunction. Cat is " << rhs->GetAge() << " years old\n"; } void ValueFunction(int age, auto_ptr rhs) { rhs->SetAge(age); cout << "In ValueFunction. Cat is " << rhs->GetAge() << " years old\n"; } int main() { auto_ptr pCat(new Cat(5)); auto_ptr pEmp(new Employee(25,5)); cout << "Main: my cat is " << pCat->GetAge() << " years old\n"; cout << "\nCalling ReferenceFunction with 7 and pCat...\n"; ReferenceFunction(7,pCat); cout << "Main: my cat is " << pCat->GetAge() << " years old\n"; //cout << "\nCalling ValueFunction with 12 and pCat...\n"; //ValueFunction(12,pCat); //cout << "Main: my cat is " << pCat->GetAge() << " years old\n"; return 0; }