unique_ptr通过特化数组类型的析构行为,自动调用delete[]释放动态数组内存,避免手动管理导致的泄漏和未定义行为。2. 推荐使用c++++14的std::make_unique<t[]>(size)创建数组智能指针,更安全简洁。3. 直接使用new t[size]构造unique_ptr<t[]>也有效,但需注意类型声明为数组类型以确保正确析构。4. 与裸指针相比,unique_ptr封装了内存管理,绑定生命周期并自动释放,提升异常安全性,减少心智负担。5. 其缺点包括固定大小、不适用于多维数组,此时可考虑std::vector作为替代方案。

unique_ptr
unique_ptr<T[]>
delete[]

使用
unique_ptr
T[]
std::make_unique
unique_ptr
new
#include <memory>
#include <iostream>
#include <vector> // 后面会提到 std::vector 作为对比
// 推荐的做法:使用 std::make_unique<T[]>(size)
void demonstrateMakeUniqueForArray() {
std::cout << "--- Using std::make_unique<int[]>(5) ---" << std::endl;
// 创建一个包含5个int的动态数组
auto intArray = std::make_unique<int[]>(5);
// 初始化并使用数组,就像普通数组一样
for (int i = 0; i < 5; ++i) {
intArray[i] = (i + 1) * 10;
std::cout << "intArray[" << i << "] = " << intArray[i] << std::endl;
}
// 当 intArray 超出作用域时,unique_ptr 的析构函数会自动调用 delete[] 释放内存
std::cout << "intArray will be deallocated automatically now." << std::endl;
}
// 传统但仍有效的方法:直接使用 new[] 构造 unique_ptr
void demonstrateNewForArray() {
std::cout << "\n--- Using std::unique_ptr<double[]>(new double[3]) ---" << std::endl;
std::unique_ptr<double[]> doubleArray(new double[3]);
doubleArray[0] = 1.1;
doubleArray[1] = 2.2;
doubleArray[2] = 3.3;
std::cout << "doubleArray[0] = " << doubleArray[0] << std::endl;
// 当 doubleArray 超出作用域时,delete[] 会被自动调用
std::cout << "doubleArray will be deallocated automatically now." << std::endl;
}
int main() {
demonstrateMakeUniqueForArray();
demonstrateNewForArray();
return 0;
}无论是
std::make_unique<T[]>(size)
std::unique_ptr<T[]>(new T[size])
delete[]

在我看来,手动管理动态数组的内存,简直是C++新手甚至老手都可能“踩坑”的重灾区。这事儿说白了,就是
new
delete
new[]
delete[]
new int
delete
new int[10]
delete[]
问题就出在这里:很多人会不小心用
delete
new[]

int* rawArray = new int[10]; // ... 使用 rawArray ... delete rawArray; // 错误!这会只释放第一个元素,或者导致未定义行为,很可能造成内存泄漏甚至程序崩溃。
这种错误的配对,会导致内存泄漏,或者更糟的是,产生未定义行为。因为
delete
delete[]
unique_ptr
unique_ptr
T[]
std::unique_ptr<T[]>
unique_ptr
delete[]
delete
这和
std::unique_ptr<T>
std::unique_ptr<T>
delete
unique_ptr
[]
// 错误示范:unique_ptr<int> 无法正确管理 int[] // std::unique_ptr<int> singleIntPtr(new int[5]); // 编译可能通过,但运行时 unique_ptr 的析构函数会调用 delete,而不是 delete[],导致未定义行为。 // 此外,singleIntPtr[0] = 10; 这样的数组访问方式会编译错误,因为 unique_ptr<T> 没有 operator[]。 // 正确示范: std::unique_ptr<int[]> arrayPtr(new int[5]); arrayPtr[0] = 10; // 正确,unique_ptr<T[]> 重载了 operator[],允许像普通数组一样访问元素。
这种设计让开发者无需再费心去区分是
delete
delete[]
unique_ptr
unique_ptr
使用
unique_ptr
优点(利):
unique_ptr
unique_ptr
unique_ptr
delete[]
缺点与考量(弊):
unique_ptr<T[]>
unique_ptr
unique_ptr<T[]>
int**
int[ROWS*COLS]
unique_ptr<T[][]>
unique_ptr
unique_ptr<unique_ptr<int[]>[]>
替代方案的思考: 在绝大多数需要动态数组的C++场景中,
std::vector
unique_ptr<T[]>
std::vector
push_back
resize
clear
std::vector
unique_ptr<T[]>
那么,何时会选择
unique_ptr<T[]>
std::vector
unique_ptr
get()
unique_ptr<T[]>
std::vector
以上就是智能指针如何管理数组资源 使用unique_ptr处理动态数组的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号