new & exception handling

G

Guest

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

Deming He

I have this code:
---------------------
try {
int *a = new int[1000000000];
} catch (...)
{
cout << "oh no!"; exit(0);
}


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

Peter Johansson

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

Guest

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

red floyd

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?

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.
 
G

Guest

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
 
M

Mike Wahler

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
 
G

Gavin Deane

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top