RAII通过对象生命周期管理资源,构造函数获取资源、析构函数释放资源,利用栈对象确定性析构保证异常安全;借助std::unique_ptr和std::shared_ptr等智能指针自动管理内存,或自定义类如FileGuard封装文件操作,确保资源在作用域结束时自动释放,防止泄漏。

RAII(Resource Acquisition Is Initialization)是C++中一种重要的资源管理机制,核心思想是将资源的生命周期绑定到对象的生命周期上。只要对象在作用域内,资源就会被正确获取;对象析构时,资源自动释放。这种方法能有效防止内存泄漏、文件句柄未关闭等问题。
RAII依赖于C++的构造函数和析构函数机制:
由于C++保证局部对象在栈上析构的确定性,RAII能实现异常安全的资源管理。
标准库中的智能指针是RAII的典型应用,避免手动调用red">new和delete。
立即学习“C++免费学习笔记(深入)”;
std::unique_ptr:独占式所有权,适用于单一所有者场景。
示例:
#include <memory>
#include <iostream>
<p>void use_unique_ptr() {
auto ptr = std::make_unique<int>(42);
std::cout << *ptr << "\n"; // 使用资源
} // ptr离开作用域,内存自动释放
std::shared_ptr:共享所有权,通过引用计数管理资源。
示例:
auto shared1 = std::make_shared<std::string>("hello");
{
auto shared2 = shared1; // 引用计数+1
} // shared2析构,引用计数-1,但资源未释放
// shared1仍有效
RAII不仅限于内存,也可用于文件、锁、网络连接等。
例如,封装文件操作:
class FileGuard {
FILE* file;
public:
explicit FileGuard(const char* path, const char* mode) {
file = std::fopen(path, mode);
if (!file) throw std::runtime_error("无法打开文件");
}
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">~FileGuard() {
if (file) std::fclose(file);
}
FILE* get() const { return file; }
// 禁止拷贝,防止资源被重复释放
FileGuard(const FileGuard&) = delete;
FileGuard& operator=(const FileGuard&) = delete;};
void use_file() { FileGuard fg("data.txt", "r"); char buffer[256]; std::fgets(buffer, 256, fg.get()); // 使用文件... } // 自动关闭文件
当代码中抛出异常时,局部对象仍会被析构,确保资源释放。
示例:
void risky_function() {
auto ptr = std::make_unique<int>(100);
FILE* fp = std::fopen("test.txt", "w");
if (!fp) throw std::runtime_error("文件打开失败");
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">// 若此处抛出异常
if (some_error()) throw std::runtime_error("出错了");
std::fclose(fp);} // 如果异常发生,ptr会释放,但fp可能泄漏
改进方式:将文件也用RAII管理。
void safe_function() {
auto ptr = std::make_unique<int>(100);
FileGuard fg("test.txt", "w"); // 使用前面定义的FileGuard
<pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">if (some_error()) throw std::runtime_error("出错了");} // 即使抛出异常,ptr和fg都会被正确清理
基本上就这些。RAII的核心是“用对象管理资源”,配合智能指针和自定义析构逻辑,能写出更安全、简洁的C++代码。
以上就是C++如何使用RAII管理资源与内存的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号