C++ Smart Pointer performance

前端 未结 6 1722
北荒
北荒 2021-02-07 05:24

How much do using smart pointers, particularly boost::shared_ptr cost more compared to bare pointers in terms of time and memory? Is using bare pointers better for performance i

6条回答
  •  花落未央
    2021-02-07 05:49

    Reference-counted smart pointers (the most common type) only cost more when you copy, create and delete them. This extra cost can be substantial if you are copying a lot, because most of them are thread-safe.

    If you just want an "auto-deleting" pointer, there is the much maligned auto_ptr, or the new and shiny (but not much supported yet) unique_ptr from C++0x.

提交回复
热议问题