(nothrow) = ? (C++)
I was perusing the cplusplus.com docs looking for tips on safer memory management, and I noticed in this example the use of the '(nothrow)' argument to the new command.
Example.
They seem to say that this '(nothrow)' tag is necessary for the command to return 0 or NULL on failure.
I had always assumed that like malloc, new would return NULL on failure automatically. This was a bit of a shock. So I went through some of my code adding this new tag, but it won't compile. It says that 'nothrow' is an unexpected argument or un-declared.
I use the tag just like in the example, so what's wrong?
This should be easy to solve, I hope.
Thanks for any solution you can offer.
Example.
They seem to say that this '(nothrow)' tag is necessary for the command to return 0 or NULL on failure.
I had always assumed that like malloc, new would return NULL on failure automatically. This was a bit of a shock. So I went through some of my code adding this new tag, but it won't compile. It says that 'nothrow' is an unexpected argument or un-declared.
I use the tag just like in the example, so what's wrong?

This should be easy to solve, I hope.
Thanks for any solution you can offer.

Code:
$ cat > test.cpp
#include <memory>
int *foo() { return new(std::nothrow) int(5); }
$ g++ -c -Wall -W -Wno-unused-parameter -Werror test.cpp
works for me...
Incidentally, it's pretty unusual that you'd actually want to do this. The default behavior of throwing std::bad_alloc is much more useful.
OneSadCookie Wrote:Incidentally, it's pretty unusual that you'd actually want to do this. The default behavior of throwing std::bad_alloc is much more useful.
A lot of my code has already been structured to check for NULL returns, so this *might* be faster.
The problem was that I was unawares that 'nothrow' was part of std.
Thanks!

Apparently, nothrow is not a member of std.
EDIT: Resolved.

Code:
/Users/gareth/Desktop/Mesh/OpenGL/../Mesh.cpp:36: error: 'nothrow' is not a member of 'std'
EDIT: Resolved.

Don't just say "fixed it"; explain what was wrong and what you changed so that people who have the same problem in the future can benefit!