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

Program aborted; new doesn't return NULL

------ foo.cpp ------
#include <iostream>
using namespace std;

int main()
{
#define FACTOR 10
for (unsigned long array_size = 1; ; array_size *= FACTOR)
{
int* p = new int[array_size];
// int* p = (int*)malloc (array_size * sizeof(int)); - works fine

cerr << array_size << " : ";

if (!(p == NULL))
{
cerr << "SUCCESS" << endl;
delete p;
}
else
{
cerr << "FAILURE" << endl;
break;
}
}
return 0;

}
---------------------
--- Compilation & Run ---

// g++ 3.3.3

$ g++ -W -Wall foo.cpp

$ a

1 : SUCCESS
10 : SUCCESS
100 : SUCCESS
1000 : SUCCESS
10000 : SUCCESS
100000 : SUCCESS
1000000 : SUCCESS
10000000 : SUCCESS
100000000 : SUCCESS
Aborted (core dumped)

-------------------------

The program is aborted. Why doesn't the program print "FAILURE"?
P.S. If we are using 'malloc' instead of 'new' the program does print "FAILURE".
--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #1
2 2361
Alex Vinokur wrote:
------ foo.cpp ------
#include <iostream>
using namespace std;

int main()
{
#define FACTOR 10
for (unsigned long array_size = 1; ; array_size *= FACTOR)
{
int* p = new int[array_size];
If you want 'new' to return NULL instead of throwing an exception, use

int* p = new (nothrow) int[array_size];
// int* p = (int*)malloc (array_size * sizeof(int)); - works fine

cerr << array_size << " : ";

if (!(p == NULL))
{
cerr << "SUCCESS" << endl;
delete p;
}
else
{
cerr << "FAILURE" << endl;
break;
}
}
return 0;

}
---------------------
--- Compilation & Run ---

// g++ 3.3.3

$ g++ -W -Wall foo.cpp

$ a

1 : SUCCESS
10 : SUCCESS
100 : SUCCESS
1000 : SUCCESS
10000 : SUCCESS
100000 : SUCCESS
1000000 : SUCCESS
10000000 : SUCCESS
100000000 : SUCCESS
Aborted (core dumped)

-------------------------

The program is aborted. Why doesn't the program print "FAILURE"?
P.S. If we are using 'malloc' instead of 'new' the program does print "FAILURE".


'new' throws 'std::bad_alloc' on failure. RTFM.

Victor
Jul 22 '05 #2

"Victor Bazarov" <v.********@comAcast.net> wrote in message news:gA******************@newsread1.mlpsca01.us.to .verio.net...
[snip]
If you want 'new' to return NULL instead of throwing an exception, use

int* p = new (nothrow) int[array_size];


Indeed. Thanks.

[snip]

--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

Jul 22 '05 #3

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

Similar topics

11
by: christopher diggins | last post by:
I am wondering if any can point me to any open-source library with program objects for C++ like there is in Java? I would like to be able to write things like MyProgram1 >> MyProgram2 >>...
4
by: Roland | last post by:
I was wondering, what happens when you have an onclick event and an error occurs in it: In an <a> element: onclick="zoomFullExtent(); return false;" I know that there is an error happening...
27
by: Mark A. Nicolosi | last post by:
I've been trying to learn C for quite a while. But I've had trouble with the lack of good quality online text (some of it's alright). But I finally bought a book on C, Practical C. I like it...
14
by: Henk | last post by:
Hi Guys, (see actual code below) I wrote a little program that is supposed to read a file (input.txt) into memory (using a stack) and then reverse the file and display it to output. It works,...
7
by: Buck Rogers | last post by:
Hi all! Newbie here. Below is an example from Teach Yourself C in 21 Days. My apologies if it is a bit long. What I don't understand is how the "get_data" function can call the...
20
by: Francine.Neary | last post by:
I am learning C, having fun with strings & pointers at the moment! The following program is my solution to an exercise to take an input, strip the first word, and output the rest. It works fine...
3
by: cs | last post by:
Hi, I'm new to C and would appreciate any feedback on the following program, asplit, which splits a file into 2 new files, putting a certain number of lines in the first file, and all the rest...
2
by: nsaffary | last post by:
hi I hava a client/server program that run correctly when i run it in one computer(local) but when I run client on a one computer and run server run on another, connection does not stablish.(I set...
41
by: c | last post by:
Hi every one, Me and my Cousin were talking about C and C#, I love C and he loves C#..and were talking C is ...blah blah...C# is Blah Blah ...etc and then we decided to write a program that...
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:
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.