I am a student and I have a little C ++ knowledge that I am trying to expand. It is rather a philosophical question. I am not trying to implement something.
WITH
#include <new> //... T * t = new (std::nothrow) T(); if(t) { //... } //...
It hides the Exception, and since working with Exceptions is more difficult than simple if(t), why not normal is new T()not considered less good practice, given that we will need to use try-catch()it to verify that the simple distribution succeeded (and if we do not, just watch how is the program dying)?
if(t)
new T()
try-catch()
What are the benefits (if any) of normal distribution newcompared to use nothrow new? Exclusion overhead is not significant in this case?
new
nothrow new
, , (, ). -, , . , , ?
, std::bad_alloc - throw n, , , (. a new int),
std::bad_alloc
throw
new int
. , .
if (t), T() , , try-catch(), , ( , , )? ( ) nothrow new? -
, ( ) - , - .
, : , , A, (A) { B, if (B) .. ". - , - RAII ( , , ).
, , , , . ( ), , . - , (, ). (, , , ). ( ) ( ), , , - , . / ( - , , !).
std:: bad_alloc , , , (, a new int),
, , , , , .
Nothrow ++, , , . , , malloc(), . , , , (, ) ++, NULL. [ , , , :]
FYI: , , , , , . , , , , , ! , . , , , ( , ).
- , nothrow .
, " ", . try/catch, .
-,
. , , , stdout - . , .
, , new nothrow, , . , , , , . , , - , , , , . new, , , , , . , ( , ), - , .
nothrow
, , , , . , , , . , , , , . , , , , , , , bad_alloc .
, , . ++ ( - ) , , .
, !
, , , , . " " , std::bad_alloc, , .
, null, , . , , , , - , . , OOM , , , , .
, : , , catch - , OOM .
catch
Symbian ++ . , OOM
T* t = new(ELeave) T();
, OOM . , , .