Disadvantages of shared_ptr

前端 未结 3 620
伪装坚强ぢ
伪装坚强ぢ 2021-02-14 07:49

With shared_ptr included in c++11, one could achieve a semi garbage-collected enviroment. Does the (inflationary?) usage come along with some disadvantages?

I could imag

3条回答
  •  别跟我提以往
    2021-02-14 08:43

    I performance is important I wold not use std::sahred_ptr. For example, to sort an array may with real pointers is 5 times faster than to sort with shared pointers.

    On the other hand shared pointer do not avoid all troubles with garbage leaks in case of circular references. Of course, this can be solved with weak_ptr, but what I mean is, that shared pointer need also be handled carfully.

    I prefer to use shared_ptr for static elements, because static elements are not deleted by a destructor of a class.

提交回复
热议问题