Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 smart pointers always instead of new/delete?

In C++11 should we always use unique_ptr or shared_ptr instead of new/delete? How is it with performance, are smart pointers much slower?

like image 434
Tom Avatar asked Mar 15 '13 12:03

Tom


1 Answers

unique_ptr doesn't (isn't supposed to) have any runtime overhead whatsoever compared to using raw pointers. shared_ptr does have some memory and time overhead (how much depends on the implementation). The practical overhead here can easily be zero if you actually need something that behaves like a shared_ptr (that is, no other implementation you'd think of would be any faster or more memory efficient).

That is not to say you'll never use new/delete in your code, but it's not something you'll do all the time.

like image 78
Cubic Avatar answered Oct 29 '22 15:10

Cubic