By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,148 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,148 IT Pros & Developers. It's quick & easy.

new & exception handling

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
<- Chameleon -> <ch******@hotmail.NOSPAM.com> wrote in message
news:bq**********@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.

Jul 22 '05 #2

P: n/a
<- 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

Jul 22 '05 #3

P: n/a
> > 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;
Jul 22 '05 #4

P: n/a
<- 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.

Jul 22 '05 #5

P: n/a
> >>>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
Jul 22 '05 #6

P: n/a

"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message
news:bq**********@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


Jul 22 '05 #7

P: n/a
"<- Chameleon ->" <ch******@hotmail.NOSPAM.com> wrote in message news:<bq**********@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


<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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.