Explain shared_ptr in C
shared_ptr is a smart pointer in C that uses reference counting to manage the life cycle of an object. 1. It allows multiple shared_ptr instances to share ownership of the same object. When the last shared_ptr pointing to the object is destroyed or reset, the object is automatically deleted. 2. The reference counting mechanism will increase the count when the shared_ptr is copied, reduce the count when the shared_ptr is destroyed or re-points to other objects, and free memory when the count returns to zero. 3. It is recommended to create it through std::make_shared when using it, avoid manual use of new and prevent mixing with naked pointers. 4. Common pitfalls include memory leaks caused by circular references, creating shared_ptr from managed bare pointers to raise undefined behavior, and high performance overhead. 5. Applicable to scenarios such as sharing ownership of multiple code parts, hoping to automatically clean up resources, or building mutually referenced data structures.
shared_ptr
in C is a smart pointer that manages the lifetime of an object using reference counting . It allows multiple shared_ptr
instances to share ownership of the same object, and the object is automatically deleted when the last remaining shared_ptr
pointing to it is destroyed or reset.

What is reference counting?
At its core, shared_ptr
works by keeping track of how many shared_ptr
objects are referring to the same underlying memory. This is called reference counting .
Each time you copy a shared_ptr
, the count increases. Each time a shared_ptr
is destroyed or assigned to point somewhere else, the count decreases. When the count hits zero, the memory is freed.

This avoids memory leaks because you don't have to manually delete the object — it's taken care of automatically.
How to use shared_ptr
Here's a basic example:

#include <memory> #include <iostream> int main() { std::shared_ptr<int> ptr1 = std::make_shared<int>(42); { std::shared_ptr<int> ptr2 = ptr1; // Reference count increases to 2 std::cout << "Inside scope: " << *ptr2 << "\n"; } // ptr2 goes out of scope, count drops to 1 std::cout << "Outside scope: " << *ptr1 << "\n"; } // ptr1 destroyed, memory freed
Some key points:
- Use
std::make_shared<T>(...)
instead ofnew
— it's safer and more efficient. - Avoid mixing raw points with
shared_ptr
unless necessary. - You can check how many owners there are with
use_count()
(mainly for debugging).
Common pitfalls with shared_ptr
Even though shared_ptr
is powerful, it has some gotchas:
- Circular references : If two objects hold
shared_ptr
s to each other, their reference counts will never drop to zero, causing a memory leak.
struct B; struct A { std::shared_ptr<B> b_ptr; }; struct B { std::shared_ptr<A> a_ptr; }; int main() { auto a = std::make_shared<A>(); auto b = std::make_shared<B>(); a->b_ptr = b; b->a_ptr = a; // Circular reference – memory never freed }
To break cycles like this, use std::weak_ptr
.
Aliasing : Creating a
shared_ptr
from a raw pointer that's already managed by anothershared_ptr
leads to undefined behavior.Performance overhead : Since
shared_ptr
tracks reference counts across copies, there's a small performance cost compared to raw points orunique_ptr
.
When to use shared_ptr
Use shared_ptr
when:
- Multiple parts of your code need to share ownership of an object.
- You want automatic cleanup without worrying about who deletes what.
- You're building data structures where objects refer to each other (but be careful with circular references).
Avoid shared_ptr
if:
- You only need one owner — prefer
unique_ptr
. - Performance is critical and reference counting overhead matters.
- You're dealing with legacy code that uses raw points extensively and can't easily be retrored.
That's basically how shared_ptr
works. It's not overly complicated, but understanding reference counting and avoiding common mistakes like circular references make a big difference in practice.
The above is the detailed content of Explain shared_ptr in C. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

When opening the software or game, a prompt suddenly appears that "the application cannot start normally (0xc0000906)" appears, and many users will be confused and don't know where to start. In fact, most of these errors are caused by corruption of system files or missing runtime libraries. Don't rush to reinstall the system. This article provides you with several simple and effective solutions to help you quickly restore the program to run. 1. What is the error of 0xc0000906? Error code 0xc0000906 is a common startup exception in Windows systems, which usually means that the program cannot load the necessary system components or running environment when running. This problem often occurs when running large software or games. The main reasons may include: the necessary runtime library is not installed or damaged. The software installation package is endless

To use regular expressions in C, you need to include header files and use the functions it provides for pattern matching and text processing. 1. Use std::regex_match to match the full string, and return true only when the entire string conforms to the pattern; 2. Use std::regex_search to find matches at any position in the string; 3. Use std::smatch to extract the capture group, obtain the complete match through matches[0], matches[1] and subsequent sub-matches; 4. Use std::regex_replace to replace the matching text, and support the capture group with references such as $1 and $2; 5. You can add an iset when constructing the regex (

The computer prompts "MsVCP71.dll is missing from the computer", which is usually because the system lacks critical running components, which causes the software to not load normally. This article will deeply analyze the functions of the file and the root cause of the error, and provide three efficient solutions to help you quickly restore the program to run. 1. What is MSVCP71.dll? MSVCP71.dll belongs to the core runtime library file of Microsoft VisualC 2003 and belongs to the dynamic link library (DLL) type. It is mainly used to support programs written in C to call standard functions, STL templates and basic data processing modules. Many applications and classic games developed in the early 2000s rely on this file to run. Once the file is missing or corrupted,

Operator overloading in C allows new behaviors of standard operators to be assigned to custom types, 1. Return new objects through member function overloading; 2. Overload = Modify the current object and return reference; 3. Friend function overloading

The basic usage of std::vector includes: 1. Declare vector; 2. Add elements with push_back(); 3. Initialize with initialization list; 4. Loop traversal with range for; 5. Access elements through index or back(); 6. Direct assignment of values to modify elements; 7. Delete the end elements with pop_back(); 8. Call size() to get the number of elements; it is recommended to use constauto& to avoid copying, pre-allocate reserve() to improve performance, and pay attention to checking that it is not empty before access. This data structure is an efficient and preferred way to handle string lists.

AbasicMakefileautomatesC compilationbydefiningruleswithtargets,dependencies,andcommands.2.KeycomponentsincludevariableslikeCXX,CXXFLAGS,TARGET,SRCS,andOBJStosimplifyconfiguration.3.Apatternrule(%.o:%.cpp)compilessourcefilesintoobjectfilesusing$

std::variant is a type-safe union introduced by C 17. It can safely hold the value of one of the specified types. It can realize secure access and type checking through methods such as std::get, std::holds_alternative, std::visit and std::get_if. Combined with std::monostate, optional values can be simulated. It is recommended to use std::visit for type distribution and avoid large type lists to improve maintainability, and ultimately ensure type safety and exception safety.

In C, the choice of std::map and std::unordered_map depends on the specific requirements. 1. Different underlying structures: std::map is implemented based on red and black trees, with keys stored in order, default ascending order, and the complexity of search and insertion is O(logn); std::unordered_map uses a hash table, unordered, and the average complexity of search and insertion is O(1), and the worst is O(n). 2. Insertion performance and memory overhead: map insertion requires maintenance of tree structure and is less efficient; unordered_map insertion is faster but consumes more memory, and can be optimized through reserve(). 3. Custom comparison function: map supports custom comparison function, unordered
