Question on auto_ptr behavior

A

Ankur Arora

I read at some C++ resource that auto_ptr makes using dynamically
allocated memory safe during exceptions.
So I tried out the following example.

#include<iostream>
#include<memory>

using namespace std;

class A
{
public:
A()
{
cout<<"\n Constructing A";
}
~A()
{
cout<<"\n Destructing A";
}
};

int main()
{
//A* aa;
auto_ptr<A> aptr(new A());

//aa = new A();
cout<<"\n main: before throw";
throw 1;
cout<<"\n main: after throw";

}

Output:-
Constructing A
main: before throw
(prompt showing debug error)

I expected that the object of A would be automatically deallocated
(i.e. destructor called) since an auto_ptr holds it but the output
suggests that it is not (in this respect the behavior is similar to
using raw pointers).

Please suggest is it the normal behavior of auto_ptr and whether my
interpretation is incorrect ?
 
A

Andy Venikov

Ankur said:
I read at some C++ resource that auto_ptr makes using dynamically
allocated memory safe during exceptions.
So I tried out the following example.

#include<iostream>
#include<memory>

using namespace std;

class A
{
public:
A()
{
cout<<"\n Constructing A";
}
~A()
{
cout<<"\n Destructing A";
}
};

int main()
{
//A* aa;
auto_ptr<A> aptr(new A());

//aa = new A();
cout<<"\n main: before throw";
throw 1;
cout<<"\n main: after throw";

}

Output:-
Constructing A
main: before throw
(prompt showing debug error)

I expected that the object of A would be automatically deallocated
(i.e. destructor called) since an auto_ptr holds it but the output
suggests that it is not (in this respect the behavior is similar to
using raw pointers).

Please suggest is it the normal behavior of auto_ptr and whether my
interpretation is incorrect ?


If a throw isn't handled by any catch, the compiler is allowed not to
call destructors and just terminate the program.

Try wrapping your code in the try/catch block.

HTH,
Andy.
 
A

Alf P. Steinbach

* Ankur Arora:
I read at some C++ resource that auto_ptr makes using dynamically
allocated memory safe during exceptions.
So I tried out the following example.

#include<iostream>
#include<memory>

using namespace std;

class A
{
public:
A()
{
cout<<"\n Constructing A";
}
~A()
{
cout<<"\n Destructing A";
}
};

int main()
{
//A* aa;
auto_ptr<A> aptr(new A());

//aa = new A();
cout<<"\n main: before throw";
throw 1;
cout<<"\n main: after throw";

}

Output:-
Constructing A
main: before throw
(prompt showing debug error)

I expected that the object of A would be automatically deallocated
(i.e. destructor called) since an auto_ptr holds it but the output
suggests that it is not (in this respect the behavior is similar to
using raw pointers).

Please suggest is it the normal behavior of auto_ptr and whether my
interpretation is incorrect ?

Since you don't catch the exception anywhere you have implementation defined
behavior. Depending on the compiler automatic (local) variables may be properly
destroyed, or not. The only sure thing is that you get a call of std::terminate.

It has nothing to do with std::auto_ptr, as you can see by replacing the pointer
with just a local variable of type A.


Cheers & hth.,

- Alf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,262
Messages
2,571,052
Members
48,769
Latest member
Clifft

Latest Threads

Top