2

I ran this program but I didn't get what this auto_ptr does and 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;
}
Vishwanath Dalvi
  • 35,388
  • 41
  • 123
  • 155

6 Answers6

8

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 (=).

Alok Save
  • 202,538
  • 53
  • 430
  • 533
1

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;
}
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • The value of `*i` may be garbage, but `auto_ptr<>::get()` returns the pointer address so garbage is never "seen". – Tony Delroy May 27 '11 at 04:10
  • "This may be a bit more revealing", yes... dereferencing a NULL pointer - `*(x.get())` - reveals all sorts of things... ;-) – Tony Delroy May 27 '11 at 04:23
  • I think, `*(x.get())` invokes undefined behaviour, because `y=x;` transfers the ownership of the memory pointed to by object `x` to the object `y`. After this, `x` points to god knows what, which is undefined behaviour. – Nawaz May 27 '11 at 04:31
1

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.

iammilind
  • 68,093
  • 33
  • 169
  • 336
  • in this program their no destructor ? – Vishwanath Dalvi May 27 '11 at 04:09
  • @Viswanathan, `auto_ptr` is a class by itself and when the object declared of `auto_ptr` (here `x` and `y`) goes out of scope from the function, their destructors are called. see the link in my answer for more deatails – iammilind May 27 '11 at 04:11
1

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

Vusak
  • 1,420
  • 9
  • 12
1

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.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
0

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) {
    ....
}
sashang
  • 11,704
  • 6
  • 44
  • 58