Program aborted; new doesn't return NULL

Discussion in 'C++' started by Alex Vinokur, Jan 17, 2005.

  1. Alex Vinokur

    Alex Vinokur Guest

    ------ 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
    Alex Vinokur, Jan 17, 2005
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 17, 2005
    #2
    1. Advertising

  3. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Jan 17, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,473
    Mike Schilling
    Mar 29, 2006
  2. Carl
    Replies:
    21
    Views:
    970
    Patricia Shanahan
    Aug 24, 2006
  3. Francois Grieu
    Replies:
    14
    Views:
    783
    Micah Cowan
    Mar 7, 2008
  4. X X
    Replies:
    4
    Views:
    337
    red floyd
    Jul 19, 2010
  5. peppermonkey
    Replies:
    1
    Views:
    224
    Gregory Brown
    Feb 10, 2007
Loading...

Share This Page