Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > new & exception handling

Reply
Thread Tools

new & exception handling

 
 
Guest
Posts: n/a
 
      11-30-2003
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


 
Reply With Quote
 
 
 
 
Deming He
Guest
Posts: n/a
 
      11-30-2003
<- Chameleon -> <(E-Mail Removed)> wrote in message
news:bqdau9$lcq$(E-Mail Removed)...
> 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.



 
Reply With Quote
 
 
 
 
Peter Johansson
Guest
Posts: n/a
 
      12-01-2003
<- 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

 
Reply With Quote
 
Guest
Posts: n/a
 
      12-01-2003
> > 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;


 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      12-01-2003
<- 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.

 
Reply With Quote
 
Guest
Posts: n/a
 
      12-02-2003
> >>>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


 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      12-02-2003

"<- Chameleon ->" <(E-Mail Removed)> wrote in message
news:bqicd9$50a$(E-Mail Removed)...
> > >>>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




 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      12-03-2003
"<- Chameleon ->" <(E-Mail Removed)> wrote in message news:<bqicd9$50a$(E-Mail Removed)>...
> > >>>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


<OT>
MSVC++6 doesn't throw std::bad_alloc when new fails, it just returns
null. This is non-standard behaviour and I don't think there's an easy
work-around
</OT>

As others have said, you can't use auto_ptr to hold an array. But you
might find something useful in the smart pointer library at
http://www.boost.org.

--
hth
GJD
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
how to handle the exception handling when NEW operator failed to allocate memory jayapal C++ 1 07-18-2007 08:11 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
SoapExtension for Global Exception handling; but prevent exception from propagating!! VSK ASP .Net Web Services 0 07-29-2003 05:39 PM



Advertisments