473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

auto_ptr's exception safe

Hello, i've got simple question about std::auto_ptr: what makes it is
exceptions safe?
Lets say i have class "int_smart_ptr" implemented like this:

class int_smart_ptr
{
private:
int *int_obj;
public:
explicit int_smart_ptr(int *_p)throw():int_obj(_p){}
~int_smart_ptr()throw(){delete int_obj;}
};

Is this class exception safe? When exception occur in scope in
int_smart_ptr was use, would destructor be call?
If yes, why?
If not, what feature of std::auto_ptr decide it is safe in case of
exception?

I hope someone understand my english :D

Regards
Kuba
--

empty

Jan 25 '06 #1
2 2305

Kuba_O wrote:
Hello, i've got simple question about std::auto_ptr: what makes it is
exceptions safe?
Simple: a proper copy ctor, assignment and destructor.
Lets say i have class "int_smart_ptr" implemented like this:

class int_smart_ptr
{
private:
int *int_obj;
public:
explicit int_smart_ptr(int *_p)throw():int_obj(_p){}
~int_smart_ptr()throw(){delete int_obj;}
};

Is this class exception safe?
No. If you copy one, and an exception is thrown, both the original and
the copy will be destroyed. Both destructors will attempt to delete the
same int*, leading to a double-deletion bug.
When exception occur in scope in int_smart_ptr was use, would
destructor be call? If yes, why?


Yes, because destructors are always called for all objects from the
scope being exited. Of course, auto_ptr has a copy ctor and an
operator= defined which avoid the double-deletion bug you have.

HTH,
Michiel Salters

Jan 25 '06 #2
25.01.2006, Mi*************@tomtom.com:
Is this class exception safe?
No. If you copy one, and an exception is thrown, both the original and
the copy will be destroyed. Both destructors will attempt to delete the
same int*, leading to a double-deletion bug.

I know that, it was just simple sample ;)
Yes, because destructors are always called for all objects from the
scope being exited. I did not know that, now everything is clear.
Thanks for inform me about such essential behavior of exceptions :D Of course, auto_ptr has a copy ctor and an
operator= defined which avoid the double-deletion bug you have.

Yeap.

out.
--

empty

Jan 25 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Jamie Burns | last post by:
Hello, I realise that I just dont get this, but I cannot see the need for auto_ptr. As far as I have read, it means that if you create an object using an auto_ptr, instead of a raw pointer, then...
23
by: guru.slt | last post by:
Hi, see this code: auto_ptr<int> int_auto_p(new int(3)); auto_ptr<int> int_auto_p2(int_auto_p.get()); Then, both auto_pointer will own the same object. That will violate the single...
11
by: James Juno | last post by:
Hello, I want to do something like this: class A { ... }; class B : public A
5
by: Raider | last post by:
Is it exception-safe to write like this: void foo(auto_ptr<Tx) { ... } void bar() { foo(auto_ptr<T>(new T(...));
6
by: bb | last post by:
Hi, Is the following safe? (it works fine and to me it looks exception safe!). void thirdPartyLibFun(T* t) { ... } void myFun() {
14
by: Pep | last post by:
I have a method in a class like this class myClass { ... ctros, dtor, etc ... string myClassMethod() { string myString = "";
10
by: Diego Martins | last post by:
I just discovered a serious issue when compiling code using auto_ptr<> and VC 8.0 consider some sort of create() function blahblah * create() { return new blahblah; } and a pretty auto_ptr
5
by: Soumen | last post by:
For one of my project, I want to use auto_ptr. But I'm facing few issues. 1. Say I've Controller class. I don't want to allow user to create object of this class unless all arguments are valid....
7
by: j.l.olsson | last post by:
Hello, I am using std::auto_ptr to try to keep ownership of resources straight. However, I became aware that exception objects must provide a copy constructor, which made me uncertain of the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.