Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should std::nothrow be used?

What is the ideal usage of std::nothrow?

like image 357
blitzkriegz Avatar asked Jan 17 '11 21:01

blitzkriegz


People also ask

What is STD Nothrow?

std::nothrow_t is an empty class type used to disambiguate the overloads of throwing and non-throwing allocation functions. std::nothrow is a constant of it.

What is std :: Bad_alloc?

Definition. std::bad_alloc is a type of exception that occurs when the new operator fails to allocate the requested space. This type of exception is thrown by the standard definitions of ​operator new (declaring a variable) and operator new[] (declaring an array) when they fail to allocate the requested storage space.


4 Answers

I'd use it only as an optimization (or code simplification) when I would otherwise have put a try-catch block immediately around a use of regular new, catching std::bad_alloc.

This is quite a rare situation, because it's rare to be able to usefully handle out-of-memory right there at the call site. Usually you allocate memory because you need it, not because you'd quite like to have it but can live without. Code that passes null pointers back up a chain of callers until eventually someone can deal with the problem isn't idiomatic C++.

It can happen though that the error genuinely can be handled immediately. For example you might be in a situation where you'll use one algorithm or technique given sufficient working space, and a different, slower algorithm or technique without. Then again, would you be allocating such working space directly with new? Not normally. And anyway, you sometimes have to be careful with that approach, because if your OS overcommits then in general you cannot handle out of memory gracefully at the application level.

Note that an expression involving std::nothrow can still throw an exception (in particular from any constructor of the object being allocated), so it's only one thing needed if you're hoping to avoid throwing an exception. You also have to ensure that the constructor won't throw.

As far as I'm concerned the days of C++ programs that don't use exceptions at all, are over. I suppose if they resumed for me, due to some particular style guide, then that's the other likely reason for needing nothrow new.

like image 117
Steve Jessop Avatar answered Sep 24 '22 00:09

Steve Jessop


As I understand it, pretty much never and none.

like image 31
Puppy Avatar answered Sep 21 '22 00:09

Puppy


Porting a C program to C++. Your C program has all those checks in there, after every malloc, and no notion of exceptions. So it's much simpler to change every malloc to new (nothrow) than to wrap every malloc in a try block.

like image 27
TonyK Avatar answered Sep 22 '22 00:09

TonyK


Perhaps if your application required nano-optimization and could not allow the overhead of exception handling, then maybe nothrow would be needed.

Bear in mind that Stroustrup is pretty adamant that the programmer can turn off overhead in C++. (As a caveat though, just because you have the choice doesn't mean you should.)

like image 35
chrisaycock Avatar answered Sep 22 '22 00:09

chrisaycock