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 ?
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 ?