What's auto_ptr?
The auto_ptr type is provided by the C++ standard library as a kind of a smart pointer that helps to avoid resource leaks when exceptions are thrown. Note that I wrote "a kind of a smart pointer." There are several useful smart pointer types. This class is smart with respect to only one certain kind of problem. For other kinds of problems, type auto_ptr does not help. So, be careful and read the following subsections.
Why need auto_ptr?
C++中没有垃圾回收机制,需要程序员动态new和delete,这是不方便并且容易出现内存泄露等等问题的。例如:
This function is a source of trouble. One obvious problem is that the deletion of the object might be forgotten (especially if you have return statements inside the function). There also is a not-so-obvious danger that an exception might occur. Such an exception would exit the function immediately without calling the delete statement at the end of the function. The result would be a memory leak or, more generally, a resource leak.
An auto_ptr is a pointer that serves as owner of the object to which it refers (if any).
这有点类似于Qt的内存处理机制,在Qt中,凡是继承于QObject的类,如果给类的对象指定父类,那么该对象会随着父类的destroyed而destroyed。
As a result, an object gets destroyed automatically when its auto_ptr gets destroyed. A requirement of an auto_ptr is that its object has only one owner.
How to use auto_ptr?
//header file for auto_ptr
#include <memory> void f()
{
//create and initialize an auto_ptr
std::auto_ptr<ClassA> ptr(new ClassA); ... //perform some operations
}
when we use auto_ptr, there is no need for delete statement any more.
An auto_ptr has much the same interface as an ordinary pointer; that is, operator * dereferences the object to which it points, whereas operator -> provides access to a member if the object is a class or a structure.
However, any pointer arithmetic (such as ++) is not defined (this might be an advantage, because pointer arithmetic is a source of trouble).
注意:不能使用赋值语句将一个普通指针赋给智能指针auto_ptr<>进行初始化。
std::auto_ptr<ClassA> ptr1(new ClassA); //OK
std::auto_ptr<ClassA> ptr2 = new ClassA; //ERROR
赋予auto_ptr一个新值的方法,新值的类型必须是auto_ptr,而不能是任何普通的指针。
std::auto_ptr<ClassA> ptr; //create an auto_ptr
ptr = new ClassA; //ERROR
ptr = std::auto_ptr<ClassA>(new ClassA); //OK, delete old object
// and own new
Transfer of Ownership by auto_ptr
不要使两个智能指针auto_ptr同时指向同一个对象,这样,该对象会被释放两次,从而产生问题!
Consider, for example, the following use of the copy constructor:
//initialize an auto_ptr with a new object
std::auto_ptr<ClassA> ptr1(new ClassA);//one //copy the auto_ptr
//- transfers ownership from ptr1 to ptr2
std::auto_ptr<ClassA> ptr2(ptr1); //second
After the first statement, ptr1 owns the object that was created with the new operator. The second statement transfers ownership from ptr1 to ptr2. So after the second statement, ptr2 owns the object created with new, and ptr1 no longer owns the object. The object created by new ClassA gets deleted exactly once — when ptr2 gets destroyed.
If ptr2 owned an object before an assignment, delete is called for that object:
//initialize an auto_ptr with a new object
std::auto_ptr<ClassA> ptr1(new ClassA);
//initialize another auto_ptr with a new object
std::auto_ptr<ClassA> ptr2(new ClassA);//own one object already ptr2 = ptr1; //assign the auto_ptr
//- delete object owned by ptr2
//- transfers ownership from ptr1 to ptr2
Source and Sink
所有权的传递暗示着auto_ptr的另外一个用途:函数可以使用auto_ptr来将所有权传递到另一个函数中去。
有两种方式可以发生这种情况! 1.A function can behave as a sink of data. This happens if an auto_ptr is passed as an argument to the function by value(传值). In this case, the parameter of the called function gets ownership of the auto_ptr. Thus, if the function does not transfer it again, the object gets deleted on function exit:
void sink(std::auto_ptr<ClassA>); //sink() gets ownership pass by value.
A function can behave as a source of data. When an auto_ptr is returned, ownership of the returned value gets transferred to the calling function. The following example shows this technique:
std::auto_ptr<ClassA> f()
{
std:: auto_ptr<ClassA> ptr(new ClassA); //ptr owns the new object
...
return ptr; //transfer ownership to calling function
} void g()
{
std::auto_ptr<ClassA> p;
for (int i=; i<; ++i) {
p = f(); //p gets ownership of the returned object
//(previously returned object of f() gets deleted)
...
}
} //last-owned object of p gets deleted
以下情况不要使用auto_ptr
auto_ptr总是包含所有权,因此,在参数表或作为返回值中,如果你不想传递所有权,就不要使用auto_ptr。
如果不恰当地使用auto_ptr,会造成极大的错误,如下例子:
//this is a bad example
template <class T>
void bad_print(std::auto_ptr<T> p) //p gets ownership of passed argument
{
//does p own an object ?
if (p.get() == NULL) {
std::cout << "NULL";
}
else {
std::cout << *p;
}
} //Oops, exiting deletes the object to which p refers
std::auto_ptr<int> p(new int);
*p = ; //change value to which p refers
bad_print (p); //Oops, deletes the memory to which p refers
*p = ; //RUNTIME ERROR
上面这个例子的意图很简单,就是打印输出参数auto_ptr<T> p的值,但是如果是以pass by value的方式调用函数,那么该函数达到意图的同时,也销毁了p指向的对象,这是极其错误的。因此,我们在使用auto_ptr的时候必须非常谨慎!
既然以传值(pass by value)的方式不行,那么我们是否可以以传引用(pass by reference)的形式调用函数呢?
You might think about passing auto_ptrs by reference instead. However, passing auto_ptrs by reference confuses the concept of ownership. A function that gets an auto_ptr by reference might or might not transfer ownership. Allowing an auto_ptr to pass by reference is very bad design and you should always avoid it.
According to the concept of auto_ptrs, it is possible to transfer ownership into a function by using a constant reference. This is very dangerous because people usually expect that an object won't get modified when you pass it as a constant reference. Fortunately, there was a late design decision that made auto_ptrs less dangerous. By some tricky implementation techniques, transfer of ownership is not possible with constant references. In fact, you can't change the ownership of any constant auto_ptr:
const std::auto_ptr<int> p(new int);
*p = ; //change value to which p refers
bad_print(p); //COMPILE-TIME ERROR
*p = ; //OK
auto_ptrs as Members
在一个自定义类中使用auto_ptr作为类的成员可以防止资源泄露。如果你使用一个auto_ptr智能指针而不是一个普通指针,那么,你不在需要在类析构函数中delete 该成员。
除此之外,auto_ptr还有一个很大的作用:一般而言,如果在类的构造函数中抛出异常,便有可能出现资源泄露的可能,
class ClassB {
private:
const std::auto_ptr<ClassA> ptr1; //auto_ptr members
const std::auto_ptr<ClassA> ptr2;
public:
//constructor that initializes the auto_ptrs
//- no resource leak possible
ClassB (ClassA val1, ClassA val2)
: ptr1 (new ClassA(val1)), ptr2(new ClassA(val2)) {
} //copy constructor
//- no resource leak possible
ClassB (const ClassB& x)
: ptr1(new ClassA(*x.ptr1), ptr2(new ClassA(*x.ptr2)) {
} //assignment operator
const ClassB& operator= (const ClassB& x) {
*ptr1 = *x.ptr1;
*ptr2 = *x.ptr2;
return *this;
} //no destructor necessary
//(default destructor lets ptr1 and ptr2 delete their objects)
...
};
对auto_ptr的误用
auto_ptrs cannot share ownership.
auto_ptrs are not provided for arrays.
auto_ptrs are not "universal smart pointers."
auto_ptrs don't meet the requirements for container elements.
一些例子
#include <iostream>
#include <memory>
using namespace std; /* define output operator for auto_ptr
* - print object value or NULL
*/
template <class T>
ostream& operator<< (ostream& strm, const auto_ptr<T>& p)
{
//does p own an object ?
if (p.get() == NULL) {
strm << "NULL"; //NO: print NULL
}
else {
strm << *p; //YES: print the object
}
return strm;
} int main()
{
auto_ptr<int> p(new int());
auto_ptr<int> q; cout << "after initialization:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl; q = p;
cout << "after assigning auto pointers:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl; *q += ; //change value of the object q owns
p = q;
cout << "after change and reassignment:" << endl;
cout << " p: " << p << endl;
cout << " q: " << q << endl;
}