开发者

how this auto_ptr program works and what it does?

I ran this program but I didn't get what this auto_ptr does and 开发者_Go百科on which basics it shows the values?

int main(int argc,char **argv)
{
    int *i= new int;
    auto_ptr<int> x(i);
    auto_ptr<int>y;
    y=x;
    count <<x.get()<<endl;
    count <<y.get()<<endl;
}


This code will print a NULL address for the first auto_ptr object and some non-NULL address for the second, showing that the source object lost the reference during the assignment (=).


auto_ptr automatically releases the memory allocated by new at the end of its scope (in this case when main exits). However this example is rather stupid because the integer never receives an actual value, so I have to imagine you're seeing garbage.

This may be a bit more revealing:

int main(int argc,char **argv)
{
    int *i= new int;
    *i = 5;
    auto_ptr<int> x(i);
    auto_ptr<int>y;

    y=x;

    count << *(x.get()) <<endl;
    count << *(y.get()) <<endl;

    *x.get() = 7;

    count << *(x.get()) <<endl;
    count << *(y.get()) <<endl;
}


In a nutshell, auto_ptr is a templated object and when its variable goes out of scope, its destructor is called and it releases the memory acquired by it. It's a one of the simpler version of smart pointers.


auto_ptr assumes ownership over the pointer you construct it with.

ie. it will automagically destroy the object pointed to once the auto_ptr itself is destroyed.

auto_ptr<int> x(i) will make x own i

auto_ptr<int> y will make y not own anything

your y=x operation will transfer ownership of i* from x to y

so now y owns i and x owns nothing, this means that when x is destroyed nothing happens, and when y is destroyed i is deleted


For this program, auto_ptr is like a normal pointer except:

  • it deletes any still-pointed-too object when it goes out of scope
  • if you copy from one auto_ptr object to another the copied-from object will "forget" about the object it has been tracking

So, x initially takes on the value of i. Then y=x effectively asks x to forget about i (by storing a NULL sentinel value instead), and y to remember i. Then printing the pointer values returned by get will show NULL/0 for x and a non-NULL value (matching i) for y. Then as main() returns, y and x leave scope, and as y is holding the non-NULL pointer value matching i, it will delete the int object, releasing the heap memory.


auto_ptr takes over ownership of the object. The important points to note are that

  • It will delete the object held internally
  • Copying/assigning auto_ptrs transfers ownership
  • It will be deprecated in c++0x and replaced with unique_ptr

For these reasons it tends to be used in conjuction with RAII (Resource Acquisition is Initializtion) paradigm. For example say you allocated a variable without storing it in an auto_ptr, then you would have to manage the deletion of the memory yourself. The example below shows that this has to be done twice, once for the good path and the once for the failed path.

A* p(new A);
try {
    ....
}
catch (std::exception& e) {
   delete p;
   return;
}
delete p;

If we used auto_ptr instead it saves us having to remember to delete the allocated memory.

auto_ptr<A> p(new A);
try {
    ....
}
catch (std::exception& e) {
    ....
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜