473,385 Members | 1,867 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,385 software developers and data experts.

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

"<- 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
"<- 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bikash | last post by:
Hello, I am a specific problem in exception handling. The code snippets is attached below. void f() { char *ptr = new char(20); throw 2; }
3
by: Master of C++ | last post by:
Hi, I am an absolute newbie to Exception Handling, and I am trying to retrofit exception handling to a LOT of C++ code that I've written earlier. I am just looking for a bare-bones, low-tech...
2
by: Nicolas Weidmann | last post by:
Hello, A long time ago (back in late 1999), libgcc did not handle exception in a thread-safe manner. In the mean time I did not have to use gcc for important things until... now. I would...
1
by: Rajesh Abraham | last post by:
I am running loop with calls different functions such as SetHomeDrive, AddUserToGroup etc. Now if any of the functions fails, I am just catching and throwing a new error like throw new...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
0
by: brunft | last post by:
I was testing this with .NET Framework 2.0. Exceptions in the Elapsed event handler of a System.Timers.Timer are ignored, they are not handled by the runtime and not reported (when running...
1
by: | last post by:
Is there a way to handle when you're on insert mode on a formview and the record you're about to save to the database already exist and insted you rather update instead of inserting and getting an...
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
2
by: Jeroen | last post by:
Here's the situation. My program will be able to start with an argument (a path to a file) and then run a batch of commands in that file. So if an argument is provided to the main method, the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.