How to know that "new" failed ?

When I allocate memory by calloc/malloc
I’ve got error when it returns NULL;

But how to know that “new” failed ?
/it returns null? throws exception?

guess what?
new returns NULL on failure

Oink,

Actually new will thow a bad_alloc exception if it fails.

Tim

…but it also returns NULL?

I want “new” to return NULL if it fails (not exception).
Is there any way to “override” new?
/to “transform it behaviour” into alloc()?

Oink,

I assume new also returns NULL in addition to throwing the exception. I’ve never actually run out of memory before nor would any of my programs actually know what to do in that case (since there is no RAM to litterally do anything unless you already pre-allocated buffers for printf’s/error handling etc).

As for overriding new, yes, you most definitely can do that. The same way as you can override anything in C++. If your planning to do this, you REALLY REALLY better know what your doing with regards to how new/delete handle memory (if you override one, you better do the other one too) because it’s not for the faint of heart. Scott Meyer’s book Effective C++, Third Edition has some excellent advice on how to do just this including hidden pitfalls.

Tim

The following program will abort (core dump) if there is not enough memory. Doesn’t matter if the program is compiled with exception enabled or not.

int main(void)
{
char *p;

 p = new char[500000000];

printf("%p\n", p );
return 0;

};

However the following program will work just fine and handle the exception properly.

int main(void)
{
char *p;

 try {
          p = new char[500000000];
     }
     catch (...)
     {
        printf("no memory\n");
      }

printf("%p\n", p );
return 0;

};

If you want new to return NULL, you must use the std::nothrow flag. new(std::nothrow) char[x] will return NULL if running out of ram.

I use a try catch in the main() to catch this and terminate the program gracefully. I consider this a critical error and just give up. That way the program is clean is not clutter with if ( p == NULL ) and try catch.

…so thanks to “nothrow” const I don’t have to override new,
but what about delete ?

does “delete” throw any exception on error (in standard / common form of use) ?

Oink,

Yes, delete can also throw exceptions. Here is a simple one.

char *p = new [10];
delete p; // fine
delete p; // throws exception since p already deleted

There may be other circumstances in which delete can throw an exception and for those you’d have to check a C++ reference book on exceptions to know what they might be.

What are you trying to accomplish with all this exception handling on new/delete. Mario’s basic try/catch block in your main will catch just any stray exceptions thrown by your program if you have a need to shutdown gracefully instead of core dumping. It makes the code far far cleaner than trying to handle them in every last routine especially since true exception handling is very hard to do in C++ compared to other languages.

Tim

Delete has a void type so you can’t check if it worked or not. Plus it also depends on what the destructor of the object does. That being said if delete throws an exception why would you want to ignore it?. It would indicate a programming error ( you can call delete with NULL pointer though that is not an error).