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

In C ++ 11, we should always use unique_ptror shared_ptrinstead of new/ delete? How does this happen with performance, smart pointers are much slower?

+5
source share
2 answers

unique_ptrdoes not (should not) have any overhead during operation compared to using raw pointers. shared_ptrhas some memory and time overhead (how much is implementation dependent). The practical overhead here can easily be zero if you really need something that behaves like shared_ptr(that is, no other implementation that you think of would be faster or more RAM).

This does not mean that you will never use the new / delete in your code, but it is not what you will do all the time.

+7
source

I would prefer shared_ptr to handle raw memory, because -

1) RAII and the calculated idiom body .

2) , .

3) , /.

+2

All Articles