+ 1

which one is better between make_unique and new

Hi As we can create smart pointer by new keyword as well as make_unique? Refer up and up1 related lines in attached code. https://code.sololearn.com/cwAAiDOKWf0C Query is which one is better? I think make_unique... Is this correct? Why ?

19th Nov 2020, 8:05 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
6 ответов
+ 1
Continue my answer, make_unique<T>() is actually the wrapper function of unique<T>(new T). But why is it safe? now let's see f(make_unique<T>(), make_unique<T>()) If the first parameter fails (constructor fails or fail to allocate for the new), the exception is thrown and new is deallocated (if allocation succeed but constructor fails). So it looks fine. Now go to the next one, if the second one fails, the exception is thrown and the new is deallocated. Wait, that's just like unique<T>(new T)! The first one won't get deallocated! It's different this time. make_unique<T>() The first make_unique<T>() is a temporary object. And the cleanup of the temporaries is correctly specified in the standard. From the standard, in 12.2/3 (unchanged from C++98): Temporary objects are destroyed as the last step in evaluating the full-expression that (lexically) contains the point where they were created. This is true even if that evaluation ends in throwing an exception.
20th Nov 2020, 12:02 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 3
make_unique is safe for creating temporary while new is not, e.g., return make_unique<int>(42). And the reason for make_unique is to tell the coder NEVER EVER use new.
19th Nov 2020, 9:19 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 2
suggest you can check it out because it has a great explanation: https://herbsutter.com/gotw/_102/
20th Nov 2020, 12:02 AM
你知道規則,我也是
你知道規則,我也是 - avatar
+ 1
What is safe in using make_unique ? Yes I read everywhere that never ever use new with introduction of new... New survived so long with so many complications of coder forgetting delete calls... Now unique ptr solving that problem with new as well...so it's making me confuse why so harsh on new with unique_ptr.... Any specific thing new is causing or just a standard to be followed with introduction of make_unique?
19th Nov 2020, 9:19 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Saying you have a function template<class T> void f(unique_ptr<T> a, unique_ptr<T> b) Now you want to pass 2 temporaries to the function, you have 2 ways: f(unique<T>(new T), unique<T>(new T)) and same with make_unique<T>() Let's break down into steps. With unique<T>(new T) the new T will be allocated before constructor calls. It's fine if either constructor fails or allocation fails because unique<T>() will throw exception and deallocate it. Let's say it succeeds, so it goes to the second parameter. Oops, the allocation or constructor fails. That's okay just throw exception at the second parameter and deallocate it. Okay, but what about the first one that succeeded? Because the exception is thrown only from the second one, when the function ends because of the exception, the first one isn't destroyed and causes memory leak. Very bad, right? So here comes make_unique<T>() (...continue)
20th Nov 2020, 12:02 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
Thank you CarrieForle ... Your explanation is awesome..... Many thanks
20th Nov 2020, 5:10 AM
Ketan Lalcheta
Ketan Lalcheta - avatar