How to dispose of unique_ptr?

跟風遠走 提交于 2019-12-11 16:42:43

问题


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_pointers 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!