Home > Backend Development > C++ > Which C Smart Pointer Should I Choose?

Which C Smart Pointer Should I Choose?

Patricia Arquette
Release: 2024-12-02 17:10:12
Original
192 people have browsed it

Which C   Smart Pointer Should I Choose?

Available C Smart Pointer Implementations and Their Comparisons

C 03

  • std::auto_ptr: Deprecated in future C standards. Limited garbage collection, ownership complications, and inability to use in STL containers.
  • std::auto_ptr_ref: Not a true smart pointer; used in conjunction with std::auto_ptr to allow copying and assignment.

C 11

  • std::unique_ptr: Replaces std::auto_ptr. Corrects weaknesses of std::auto_ptr, allowing work with arrays, lvalue protection, and use in STL containers.
  • std::shared_ptr: Reference-counted smart pointer. Supports aliasing and pointer arithmetic.
  • std::weak_ptr: Non-owning reference to an object owned by a std::shared_ptr. Prevents indefinite cyclic reference counts.

Boost

  • boost::shared_ptr: Popular shared reference-counted smart pointer. High performance and versatility.
  • boost::weak_ptr: Non-owning reference to a boost::shared_ptr.
  • boost::scoped_ptr: Simple smart pointer alternative to boost::shared_ptr with less overhead.
  • boost::intrusive_ptr: Custom smart pointer for creating your own smart pointer-compatible classes.
  • boost::shared_array: Shared smart pointer for arrays.
  • boost::scoped_array: Non-copyable smart pointer for arrays.

Qt

  • QPointer: Weak smart pointer for QObject and derived classes. Deprecated.
  • QSharedDataPointer: Strong smart pointer comparable to boost::intrusive_ptr, with thread safety.
  • QExplicitlySharedDataPointer: Version 2.0 of QSharedDataPointer with enhanced control over detachment.
  • QSharedPointer: Atomic reference-counting, thread-safe, sharable smart pointer.
  • QWeakPointer: Non-owning reference to a QSharedPointer.
  • QScopedPointer: Single-owner smart pointer based on boost::scoped_ptr, suitable for exception-safe code and compatibility.

The above is the detailed content of Which C Smart Pointer Should I Choose?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template