Home > Article > Backend Development > Why Doesn\'t the C Standard Include a Non-Atomic `std::shared_ptr`?
Non-Atomic Smart Pointers: Exploring the Absence in the C Standard
In the realm of C memory management, the std::shared_ptr stands as an exceptional smart pointer due to its atomic operations. This raises the question: why isn't there a non-atomic counterpart within the standard memory library?
1. Non-Atomic Alternatives to std::shared_ptr
While the std::shared_ptr offers atomic behavior, some scenarios may not warrant such overhead. To address this, external libraries have emerged, providing non-atomic reference-counted smart pointers. However, the standard memory library itself lacks such offerings.
2. The Absence in C 11: Reasons and Debate
Despite the perceived utility, a proposition for a non-atomic version of std::shared_ptr was rejected during the C 11 standardization process. Several arguments fueled this decision:
Conclusion
The absence of a non-atomic std::shared_ptr in the C standard is the result of a deliberate decision based on concerns about potential misuse, consistency, performance, and customization flexibility. While external libraries can provide such alternatives, the standard itself has prioritized the benefits of a universal, atomic smart pointer for memory management.
The above is the detailed content of Why Doesn\'t the C Standard Include a Non-Atomic `std::shared_ptr`?. For more information, please follow other related articles on the PHP Chinese website!