new & exception handling



 DEVELOP > c-Plus-Plus > new & exception handling

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "- Chameleon -"
Date: 30 Nov 2003 11:55:30 AM
Object: new & exception handling
I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?
============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)
if I want automatic deletion of int array inside a class constructor on
exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------
thanks
.

User: "Deming He"

Title: Re: new & exception handling 30 Nov 2003 01:21:33 PM
<- Chameleon -> <cham_gss@hotmail.NOSPAM.com> wrote in message
news:bqdau9$lcq$1@nic.grnet.gr...

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?

I double-checked B. Stroustrup's book on this: the defaulted behavior of
exhausting store should be throwing a bad_alloc exception. As to not
catching the 'bad_alloc' in your case it could be an evidence of deviation
of the compiler from standards.

============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)

The object pointed by an auto_ptr will be implicitly deleted at the end of
the scope of the said auto_ptr. Thus, you don't have to call delete in case
you forget - this save you from possible memory leak.

if I want automatic deletion of int array inside a class constructor on
exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------

I don't understand what you mean here...Also, is that a typo (new int(100))
if you wanted an array of int?
Generally you cannot use auto_ptr with an array; use other template classes
instead.
.

User: "Peter Johansson"

Title: Re: new & exception handling 01 Dec 2003 05:28:41 AM
<- Chameleon -> wrote:

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?
============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)
if I want automatic deletion of int array inside a class constructor on
exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------

thanks


The compiler I use (cxx) throws an exception only if the source code is
compiled with an option to the compiler telling it to use std new.
Otherwise, 0 is returned instead of throwing an exception. You probably
need to tell your compiler to use std new.
/ Peter
.
User: "- Chameleon -"

Title: Re: new & exception handling 01 Dec 2003 02:55:54 PM

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?
============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)
if I want automatic deletion of int array inside a class constructor on
exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------

thanks


The compiler I use (cxx) throws an exception only if the source code is
compiled with an option to the compiler telling it to use std new.
Otherwise, 0 is returned instead of throwing an exception. You probably
need to tell your compiler to use std new.

above of this code I use
using namespace std;
.
User: "red floyd"

Title: Re: new & exception handling 01 Dec 2003 02:57:56 PM
<- Chameleon -> wrote:

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?
============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)
if I want automatic deletion of int array inside a class constructor on
exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------

thanks



The compiler I use (cxx) throws an exception only if the source code is
compiled with an option to the compiler telling it to use std new.
Otherwise, 0 is returned instead of throwing an exception. You probably
need to tell your compiler to use std new.



above of this code I use
using namespace std;


No. auto_ptr is not for arrays. The destructor calls delete, not delete[].
As for the lack of exception on new , what compiler are you using? You also could look specifically for a std::bad_alloc exception.
.
User: "- Chameleon -"

Title: Re: new & exception handling 02 Dec 2003 09:51:19 AM

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?
============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)
if I want automatic deletion of int array inside a class constructor on
exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------


The compiler I use (cxx) throws an exception only if the source code is
compiled with an option to the compiler telling it to use std new.
Otherwise, 0 is returned instead of throwing an exception. You probably
need to tell your compiler to use std new.



above of this code I use
using namespace std;



No. auto_ptr is not for arrays. The destructor calls delete, not

delete[].


As for the lack of exception on new , what compiler are you using? You

also could look specifically for a std::bad_alloc exception.
I use MS VC++ 6
.
User: "Mike Wahler"

Title: Re: new & exception handling 02 Dec 2003 12:45:19 PM
"<- Chameleon ->" <cham_gss@hotmail.NOSPAM.com> wrote in message
news:bqicd9$50a$1@nic.grnet.gr...

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}
----------------------
new returns 0 but no exception occur in this error. Why?
============
I learn today about auto_ptr. What is the use of this? (I have not
understand it well)
if I want automatic deletion of int array inside a class constructor

on

exception I must use it like this?
-------------------------
auto_ptr<int> array_of_int(new int(100));
-------------------------


The compiler I use (cxx) throws an exception only if the source code

is

compiled with an option to the compiler telling it to use std new.
Otherwise, 0 is returned instead of throwing an exception. You

probably

need to tell your compiler to use std new.



above of this code I use
using namespace std;



No. auto_ptr is not for arrays. The destructor calls delete, not

delete[].


As for the lack of exception on new , what compiler are you using? You

also could look specifically for a std::bad_alloc exception.

I use MS VC++ 6

VC++6 default behavior upon failure of operator new is
nonstandard (but there is a way to work around that).
See: http://tinyurl.com/xef9
-Mike
.






  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER