问题
I was looking at some arduino code which said :
std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
Normally a unique pointer is created using make_unique.
So I was wondering what I need to do to dispose of this pointer ?
回答1:
You don't need to do anything. This code does almost the same thing as make_unique
. I'd recommend you not to write code like this yourself (make_unique
is better), but if you can't change this line, you are unlikely to notice the difference.
When unique_ptr
is initialized with a raw pointer and when the expression is a bit more complicated than in this case, memory leak can occur. make_unique
prevents that possibility.
With shared_pointer
s it's also slightly less efficient to initialize it with a raw pointer than to use make_shared
.
回答2:
I think make_unique is introduced in c++14, before that, the systax you posted is the standard way to create a unique_ptr. You don't need to do anything to dispose the raw pointer and unique_ptr.
来源:https://stackoverflow.com/questions/58926590/how-to-dispose-of-unique-ptr