Guid
Guid

Reputation: 2216

Is it useful to test the return of "new" in C++?

I usually never see test for new in C++ and I was wondering why.

Foo *f = new Foo;

// f is assumed as allocated, why usually, nobody test the return of new?

Upvotes: 28

Views: 4592

Answers (6)

David Holm
David Holm

Reputation: 17970

As per the current standard, the argumentless version of new never returns NULL, it throws a std::bad_alloc instead. If you don't want new to throw(as per the old standard) but rather return NULL you should pass std::nothrow, such as

Foo* foo = new(std::nothrow) Foo;

Of course, if you have a very old or possibly broken toolchain it might not follow the standard.

Upvotes: 50

n-alexander
n-alexander

Reputation: 15363

  1. new throws std::bad_alloc by default. If you use default, checking for null is obsolete, but handling exceptions is necessary. This default behavior is consistent with C++ exception safety paradigm - it usually provides that an object is either constructed or not allocated

  2. if you override default by using new (std::nothrow), checking on null is necessary. "New" both allocates and commits pages, so it is possible to run out of memory, either because you ran out of page descriptors, or because there's no physical memory available

  3. research your OS's memory management. C/C++ reference machine does not know how your OS manages memory, so relying on language alone is not safe. For example of memory allocation gone bad, read on C malloc() + Linux overcommit

Upvotes: 5

João Augusto
João Augusto

Reputation: 2305

It all depends on your complier VC++ up to version 6 gives NULL if the new operator fails, on a non MFC application.

Now the problem gets bigger when you use for example STL with VC++ 6, because the STL goes with the standards it will never test for NULL when he needs to get some memory, and guess what will happen under low memory conditions....

So for everybody that still uses VC++ 6 and STL check this article for a Fix. Don't Let Memory Allocation Failures Crash Your Legacy STL Application

Upvotes: 7

Burkhard
Burkhard

Reputation: 14738

As quoted here

"In compilers conforming to the ISO C++ standard, if there is not enough memory for the allocation, the code throws an exception of type std::bad_alloc. All subsequent code is aborted until the error is handled in a try-catch block or the program exits abnormally. The program does not need to check the value of the pointer; if no exception was thrown, the allocation succeeded."

Upvotes: 3

Chris Becke
Chris Becke

Reputation: 36101

It all depends which version of C++ the code targets. The c++ specification for a long time now has stated that, by default at least, failures in new will cause a c++ exception, so any code performing a test would be entirely redundant. Most programming nowadays also targets virtual memory operating systems where its almost impossible to run out of memory, AND an out-of-memory condition is so fatal anyway that just letting the application crash on the next NULL access is as good a way of any of terminating.

Its only really in embedded programming, where exception handling is deemed to be too much of an overhead, and memory is very limited, that programmers bother to check for new failures.

Upvotes: 4

Windows programmer
Windows programmer

Reputation: 8065

Usually no one tests the return of new in new code because Visual Studio now throws the way the standard says.

In old code if a hack has been done to avoid throwing then you'd still better test.

Upvotes: 0

Related Questions