使用unique_ptr<T[]>而非unique_ptr<T>管理数组,是因为前者会正确调用delete[]释放内存,避免内存泄漏和未定义行为。unique_ptr<T[]>专为数组设计,确保析构时调用数组形式的delete[],而unique_ptr<T>仅调用delete,导致数组对象析构不完整。C++中单对象与数组的内存管理机制不同,必须匹配使用new/delete或new[]/delete[]。若用unique_ptr<T>管理new[]分配的数组,仅首元素被析构,其余内存泄露或损坏。因此,为保证安全,必须使用unique_ptr<T[]>形式。make_unique<T[]>(size)是创建此类智能指针的推荐方式,兼具简洁性与异常安全。该方式对基本类型零初始化,对类类型调用默认构造函数。若类无默认构造函数,需手动处理或改用vector。此外,unique_ptr<T[]>不支持operator*或operator->,仅能通过operator[]访问元素,且数组大小固定,不可动态扩容。高级用法包括自定义deleter以管理非new[]分配的内存(如malloc),通过.get()获取原始指针供C风格函数使用,以及通过移动语义转移所有权

C++中
unique_ptr
unique_ptr<T[]>
unique_ptr<T>
delete[]
unique_ptr
unique_ptr
new[]
delete[]
delete[]
#include <iostream>
#include <memory> // 包含 unique_ptr
#include <vector> // 也会提及 std::vector
// 一个简单的类,用来观察构造和析构
struct MyObject {
int id;
MyObject(int i = 0) : id(i) { std::cout << "MyObject " << id << " constructed.\n"; }
~MyObject() { std::cout << "MyObject " << id << " destructed.\n"; }
};
int main() {
// 1. 正确使用 unique_ptr<T[]> 管理数组
// C++14 引入的 std::make_unique 是创建 unique_ptr 的首选方式
// 它对数组类型同样适用,并且提供了异常安全保证
auto myIntArray = std::make_unique<int[]>(5); // 创建一个包含5个int的数组
for (int i = 0; i < 5; ++i) {
myIntArray[i] = i * 10; // 通过 operator[] 访问元素
}
std::cout << "First element of myIntArray: " << myIntArray[0] << "\n";
std::cout << "Third element of myIntArray: " << myIntArray[2] << "\n";
// 2. 管理自定义类型数组
auto myObjectArray = std::make_unique<MyObject[]>(3); // 创建3个MyObject对象
myObjectArray[0].id = 100;
myObjectArray[1].id = 200;
myObjectArray[2].id = 300;
std::cout << "MyObjectArray element 0 ID: " << myObjectArray[0].id << "\n";
// unique_ptr 离开作用域时,会自动调用 delete[] 释放内存
// 对于 MyObject 数组,会依次调用每个 MyObject 的析构函数
// 3. 也可以直接用 new[] 和 unique_ptr 构造函数,但不如 make_unique 异常安全
// std::unique_ptr<double[]> myDoubleArray(new double[4]);
// myDoubleArray[0] = 1.1;
// 4. 获取原始指针,但所有权仍在 unique_ptr 手中
double* rawPtr = new double[4];
std::unique_ptr<double[]> myDoubleArray(rawPtr);
myDoubleArray[0] = 1.1;
std::cout << "Raw pointer access: " << myDoubleArray.get()[0] << "\n";
// 注意:不要手动 delete rawPtr,它由 myDoubleArray 管理
std::cout << "End of main function.\n";
return 0;
}unique_ptr<T[]>
unique_ptr<T>
这背后其实有个很简单的道理:C++ 的内存释放机制是分单对象和数组的。当你
new
delete
new[]
delete[]
delete
delete
delete[]
unique_ptr<T>
delete
unique_ptr<T[]>
delete[]
std::unique_ptr<int> ptr(new int[10]);
ptr
delete ptr.get();
delete[] ptr.get();
[]
立即学习“C++免费学习笔记(深入)”;
std::make_unique
std::make_unique
unique_ptr
auto my_array = std::make_unique<T[]>(size);
这里的
size
正确用法示例:
#include <memory>
#include <iostream>
int main() {
// 创建一个包含10个整数的数组,并自动初始化为0
auto int_array = std::make_unique<int[]>(10);
std::cout << "int_array[0] before assignment: " << int_array[0] << std::endl; // 输出0
// 创建一个包含5个自定义对象的数组
struct Widget {
int id;
Widget() : id(0) { std::cout << "Widget default constructed.\n"; }
Widget(int i) : id(i) { std::cout << "Widget " << id << " constructed.\n"; }
~Widget() { std::cout << "Widget " << id << " destructed.\n"; }
};
auto widget_array = std::make_unique<Widget[]>(5); // 调用5次默认构造函数
widget_array[0].id = 1;
widget_array[1].id = 2;
std::cout << "widget_array[0].id: " << widget_array[0].id << std::endl;
// 注意:make_unique<T[]>(size) 对于基本类型会进行零初始化,
// 对于类类型会调用默认构造函数。
// 如果类没有默认构造函数,或者你需要自定义初始化逻辑,
// 你可能需要手动循环赋值,或者考虑使用 std::vector。
// 错误示范(编译不通过或行为不符合预期):
// auto bad_array = std::make_unique<int>(10); // 这是创建单个 int,不是数组
// auto another_bad = std::make_unique<int[]>(); // 数组大小必须指定
return 0;
}注意事项:
make_unique<T[]>(size)
int
double
make_unique
new T[size]
unique_ptr
std::vector
make_unique
unique_ptr
new
unique_ptr
std::unique_ptr<T[]>(new T[size])
new T[size]
make_unique<T[]>()
unique_ptr
即便
unique_ptr<T[]>
常见陷阱:
unique_ptr<T>
unique_ptr<T[]>
[]
// 错误示范:会导致未定义行为和内存泄露 // std::unique_ptr<int> bad_ptr(new int[10]); // 编译可能通过,但运行时会出错
或
:**
类型没有定义
或
。因为它代表的是一个数组的首地址,而不是一个单个对象。你只能通过
auto my_array = std::make_unique<int[]>(5); // int val = *my_array; // 编译错误! // my_array->some_method(); // 编译错误! int val = my_array[0]; // 正确
unique_ptr<T[]>
std::vector
std::vector
unique_ptr<T[]>
高级内存管理技巧:
自定义 Deleter:
unique_ptr
new
new[]
#include <memory>
#include <iostream>
#include <cstdlib> // For malloc and free
// 假设我们有一个C风格的函数,它返回一个由malloc分配的int数组
int* create_c_array(size_t size) {
return static_cast<int*>(std::malloc(size * sizeof(int)));
}
// 自定义deleter,用于free C风格内存
struct FreeDeleter {
void operator()(int* ptr) const {
std::cout << "Calling custom FreeDeleter for array.\n";
std::free(ptr);
}
};
int main() {
// 使用自定义deleter管理malloc分配的内存
std::unique_ptr<int[], FreeDeleter> managed_c_array(create_c_array(10));
if (managed_c_array) {
managed_c_array[0] = 100;
std::cout << "Managed C array element 0: " << managed_c_array[0] << std::endl;
}
// 也可以使用lambda作为deleter
auto lambda_managed_array = std::unique_ptr<double[], decltype([](double* p){
std::cout << "Calling lambda deleter for array.\n";
std::free(p);
})>(static_cast<double*>(std::malloc(5 * sizeof(double))));
if (lambda_managed_array) {
lambda_managed_array[0] = 3.14;
std::cout << "Lambda managed array element 0: " << lambda_managed_array[0] << std::endl;
}
return 0;
}这里需要注意的是,自定义 deleter 的类型会成为
unique_ptr
获取原始指针 (.get()
unique_ptr
T*
.get()
unique_ptr
delete
void process_raw_array(int* arr, size_t size) {
for (size_t i = 0; i < size; ++i) {
arr[i] *= 2;
}
}
int main() {
auto my_array = std::make_unique<int[]>(5);
for (int i = 0; i < 5; ++i) my_array[i] = i + 1;
process_raw_array(my_array.get(), 5); // 传递原始指针
std::cout << "After processing: " << my_array[0] << ", " << my_array[1] << std::endl;
return 0;
}所有权转移:
unique_ptr
unique_ptr<T[]>
unique_ptr
std::unique_ptr<int[]> create_and_return_array(size_t size) {
auto arr = std::make_unique<int[]>(size);
for (size_t i = 0; i < size; ++i) {
arr[i] = static_cast<int>(i * 10);
}
return arr; // 返回时发生移动
}
int main() {
auto received_array = create_and_return_array(3);
std::cout << "Received array element 0: " << received_array[0] << std::endl;
std::unique_ptr<int[]> another_array;
another_array = std::move(received_array); // 显式移动所有权
// received_array 现在为空
std::cout << "Another array element 1: " << another_array[1] << std::endl;
return 0;
}总的来说,
unique_ptr<T[]>
std::vector
以上就是C++unique_ptr数组操作与内存管理注意事项的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号