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

new throws or returns?

I am confused on this:

When "new" fails, what happens?
- throws bad_alloc
- returns 0

I found many docs with first, many with second and one with both(!) cases.
thanks!
Jan 5 '07 #1
6 1660

Chameleon napsal:
I am confused on this:

When "new" fails, what happens?
- throws bad_alloc
- returns 0

I found many docs with first, many with second and one with both(!) cases.
thanks!
When new int[1000000] fails, it throw std::bad_alloc.
When new(std::nothrow)int[1000000] fails, it returns 0

Jan 5 '07 #2
Ondra Holub wrote:
Chameleon napsal:
I am confused on this:

When "new" fails, what happens?
- throws bad_alloc
- returns 0

I found many docs with first, many with second and one with both(!) cases.
thanks!

When new int[1000000] fails, it throw std::bad_alloc.
When new(std::nothrow)int[1000000] fails, it returns 0
Right, on standard-compliant implementations. There are non-standard
implementations out there that return null on a plain new because they
don't support exceptions or are simply out of date.

Cheers! --M

Jan 5 '07 #3
Chameleon wrote:
I am confused on this:

When "new" fails, what happens?
- throws bad_alloc
- returns 0

I found many docs with first, many with second and one with both(!) cases.
The first happens.
Jan 5 '07 #4
On Fri, 05 Jan 2007 15:46:42 +0200, Chameleon wrote:
>I am confused on this:
When "new" fails, what happens?
- throws bad_alloc
- returns 0
Most probably neither nor. operator new allocates memory and calls a
constructor. If construction fails a constructor specific exception is
(better, may be) thrown. Out of memory is usually handled by a
new_handler that just terminates the application (you cannot 'handle'
out of memory). The std::bad_alloc exception is a textbook artefact
that is never seen in the real world.

Best wishes,
Roland Pibinger
Jan 5 '07 #5
Roland Pibinger wrote:
On Fri, 05 Jan 2007 15:46:42 +0200, Chameleon wrote:
>I am confused on this:
When "new" fails, what happens?
- throws bad_alloc
- returns 0

Most probably neither nor. operator new allocates memory and calls a
constructor. If construction fails a constructor specific exception is
(better, may be) thrown. Out of memory is usually handled by a
new_handler that just terminates the application (you cannot 'handle'
out of memory). The std::bad_alloc exception is a textbook artefact
that is never seen in the real world.
No, bad_alloc is mandated by the Standard. Just because *you* aren't
using standard-compliant compiler, don't assume the same for the rest of us.
Jan 5 '07 #6


On Jan 5, 6:06 pm, red floyd <no.s...@here.dudewrote:
Roland Pibinger wrote:
On Fri, 05 Jan 2007 15:46:42 +0200, Chameleon wrote:
I am confused on this:
When "new" fails, what happens?
- throws bad_alloc
- returns 0
Most probably neither nor. operator new allocates memory and calls a
constructor. If construction fails a constructor specific exception is
(better, may be) thrown. Out of memory is usually handled by a
new_handler that just terminates the application (you cannot 'handle'
out of memory). The std::bad_alloc exception is a textbook artefact
that is never seen in the real world.No, bad_alloc is mandated by the Standard. Just because *you* aren't
using standard-compliant compiler, don't assume the same for the rest of us.
That was the impression I got from reading the online Dinkumware C++
documentation.

To the orginal poster, the dinkumware site gives a very good
description of this.
Just search on "new handler". Also, Meyer's Effective C++, 3rd
Edition, goes
over this in great detail.

Jan 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Dr. Laurence Leff | last post by:
I am writing a Java program to read in XML file, modify some elements slightly, and then write it out. That XML file is prepared in Docbook. It works fine, except that it is disturbing the...
0
by: Sachin Narasimhan via .NET 247 | last post by:
(Type your message here) Hi, I am at my wits end. It would be great if someone can help. Wehave a Stored Function(Oracle) that returns a boolean that wecannot change to return something else and we...
1
by: Koji Ishii | last post by:
I'm trying to read response from a web server using XmlTextReader. Here's a code snippet. WebResponse rs = this.WebRequest.GetResponse(); Stream stm = rs.GetResponseStream(); XmlTextReader...
3
by: Sébastien Ros | last post by:
During the process of an XML document, I have to make a SelectNodes() call on an XmlDocument. It returns an isntance of XPathNodeList. I can call it several times but at one point, the result can't...
1
by: Lars Stenberg | last post by:
Hello! Im sitting here and trying to retreive som avi information, but the invoke of the AVIFileInfo throws a nullpointer exception. snipp ------- public struct AVIFILEINFO { public long...
5
by: Niall | last post by:
I have an unmanaged C++ ray tracer which I am working to put a C# front end on. It runs fine as just the unmanaged code. I have made a MC++ wrapper DLL to expose the required types to the C#...
0
by: mjheitland | last post by:
When I pass my directories recursively on a directory structure with paths 260 chars I get the following exception (see line marked with ***), although I am using only ShortPathNames through...
1
thatos
by: thatos | last post by:
I have the class TableCollection which creates tables for data store in the given direcoty, the method which reads this goes like this. public void read(String dirname,String regex) throws...
3
by: Taras_96 | last post by:
Hi everyone, In the documentation it says that set_error_handler will return 'Returns a string containing the previously defined error handler (if any), or NULL on error.' However, in my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.