Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Program aborted; new doesn't return NULL

Reply
Thread Tools

Program aborted; new doesn't return NULL

 
 
Alex Vinokur
Guest
Posts: n/a
 
      01-17-2005
------ 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



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-17-2005
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
 
Reply With Quote
 
 
 
 
Alex Vinokur
Guest
Posts: n/a
 
      01-17-2005

"Victor Bazarov" <(E-Mail Removed)> wrote in message news:gAUGd.36145$(E-Mail Removed)01.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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"return delete (new int)" compile but "return delete (new X X C++ 4 07-19-2010 05:47 PM
Can realloc(p,0) return NULL when p is non-NULL and memory aplenty? Francois Grieu C Programming 14 03-07-2008 05:40 PM
Why use "return (null);" instead of "return null;" ? Carl Java 21 08-24-2006 04:33 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments