Program aborted; new doesn't return NULL



 DEVELOP > c-Plus-Plus > Program aborted; new doesn't return NULL

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Alex Vinokur"
Date: 17 Jan 2005 01:38:08 PM
Object: 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
.

User: "Victor Bazarov"

Title: Re: Program aborted; new doesn't return NULL 17 Jan 2005 01:44:12 PM
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
.
User: "Alex Vinokur"

Title: Re: Program aborted; new doesn't return NULL 17 Jan 2005 01:51:05 PM
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message news:gAUGd.36145$NC6.9783@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
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER