在现代C++开发中,C++ memory库扮演着至关重要的角色。它提供了一套强大而安全的工具,帮助开发者高效、安全地管理内存资源,避免常见的内存泄漏和悬空指针问题。本教程将从零开始,带你全面了解C++ memory库的核心组件——尤其是智能指针,并掌握如何在实际项目中应用它们。

C++11标准引入了<memory>头文件,其中定义了多种用于C++内存管理的工具,最核心的就是三种智能指针:
std::unique_ptr:独占所有权的智能指针std::shared_ptr:共享所有权的智能指针std::weak_ptr:配合shared_ptr使用的弱引用指针,用于打破循环引用这些智能指针通过RAII(Resource Acquisition Is Initialization)机制,在对象生命周期结束时自动释放所管理的内存,极大提升了程序的安全性和可维护性。
std::unique_ptr确保同一时间只有一个指针拥有资源的所有权。它不能被复制,但可以被移动(move),非常适合“唯一拥有”的场景。
#include <iostream>#include <memory>class MyClass {public: MyClass(int val) : value(val) { std::cout << "MyClass constructed with value: " << value << std::endl; } ~MyClass() { std::cout << "MyClass destructed!" << std::endl; } void print() { std::cout << "Value: " << value << std::endl; }private: int value;};int main() { // 创建 unique_ptr std::unique_ptr<MyClass> ptr = std::make_unique<MyClass>(42); ptr->print(); // 移动所有权(不能复制!) std::unique_ptr<MyClass> ptr2 = std::move(ptr); // 此时 ptr 已为空 if (!ptr) { std::cout << "ptr is now null." << std::endl; } ptr2->print(); // 程序结束时,ptr2 自动析构 MyClass 对象 return 0;}std::shared_ptr允许多个指针共享同一个对象。它内部维护一个引用计数器,当最后一个shared_ptr被销毁时,才会释放所管理的对象。这是实现“多对一”资源管理的理想选择。
#include <iostream>#include <memory>class Data {public: Data(int n) : num(n) { std::cout << "Data created: " << num << std::endl; } ~Data() { std::cout << "Data destroyed: " << num << std::endl; } int get() const { return num; }private: int num;};int main() { // 创建 shared_ptr std::shared_ptr<Data> sp1 = std::make_shared<Data>(100); { std::shared_ptr<Data> sp2 = sp1; // 引用计数变为2 std::cout << "Reference count: " << sp1.use_count() << std::endl; std::shared_ptr<Data> sp3 = sp2; // 引用计数变为3 std::cout << "Reference count: " << sp1.use_count() << std::endl; } // sp2 和 sp3 被销毁,引用计数降为1 std::cout << "After scope, reference count: " << sp1.use_count() << std::endl; return 0; // sp1 销毁,引用计数归零,Data 对象被释放}当两个shared_ptr互相持有对方时,就会形成循环引用,导致引用计数永远不会归零,从而引发内存泄漏。std::weak_ptr不增加引用计数,仅作为观察者存在,可有效打破这种循环。
#include <iostream>#include <memory>class B;class A {public: std::shared_ptr<B> b_ptr; ~A() { std::cout << "A destroyed" << std::endl; }};class B {public: std::weak_ptr<A> a_ptr; // 使用 weak_ptr 避免循环引用 ~B() { std::cout << "B destroyed" << std::endl; }};int main() { auto a = std::make_shared<A>(); auto b = std::make_shared<B>(); a->b_ptr = b; b->a_ptr = a; // weak_ptr 不增加 a 的引用计数 std::cout << "a use_count: " << a.use_count() << std::endl; // 输出 1 return 0; // a 和 b 都能正常析构}std::make_unique和std::make_shared创建智能指针,更安全且性能更好unique_ptr(开销更小)shared_ptr的循环引用问题,必要时使用weak_ptr通过本文,你应该已经掌握了C++ memory库的核心概念,特别是unique_ptr、shared_ptr和weak_ptr的使用方法。合理运用这些智能指针,不仅能提升代码安全性,还能显著减少内存泄漏风险。记住,现代C++编程中,手动new/delete应尽量避免,让智能指针为你自动管理内存吧!
关键词回顾:C++ memory库、智能指针、C++内存管理、unique_ptr shared_ptr。
本文由主机测评网于2025-12-22发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20251211216.html