正确声明和初始化 unique_ptr 管理数组需使用 std::unique_ptr<T[]> 形式,并通过 new T[size] 初始化,例如 std::unique_ptr<int[]> arr(new int[10]);,这样析构时会自动调用 delete[] 释放内存,避免内存泄漏或崩溃。常见错误是使用 std::unique_ptr<T> 管理数组,导致 delete 与 delete[] 不匹配,引发未定义行为。unique_ptr 相比原始指针优势在于自动内存管理、异常安全、明确独占所有权,防止内存泄漏和悬挂指针。C++14 的 std::make_unique 不直接支持数组,但可自定义 make_unique_array 辅助函数实现类似功能。unique_ptr 管理数组时不可复制,所有权转移需使用 std::move,若需共享所有权应使用 std::shared_ptr,并配合自定义删除器或 std::default_delete<T[]> 确保正确调用 delete[]。

C++的
unique_ptr
unique_ptr<T[]>
unique_ptr<T>
unique_ptr
delete[]
delete
解决方案
使用
std::unique_ptr<T[]>
如何正确声明和初始化 unique_ptr
立即学习“C++免费学习笔记(深入)”;
声明时使用
unique_ptr<T[]>
new T[size]
#include <iostream>
#include <memory>
int main() {
// 创建一个可以管理 int 数组的 unique_ptr
std::unique_ptr<int[]> arr(new int[10]);
// 初始化数组
for (int i = 0; i < 10; ++i) {
arr[i] = i * 2;
}
// 访问数组元素
for (int i = 0; i < 10; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
// unique_ptr 会自动释放数组内存
return 0;
}如何避免使用 unique_ptr
最常见的错误是使用
unique_ptr<T>
unique_ptr<T[]>
delete
delete[]
unique_ptr
例如,以下代码是错误的:
#include <memory>
int main() {
// 错误:使用了 unique_ptr<int> 来管理数组
// 这会导致 delete 和 delete[] 不匹配
// std::unique_ptr<int> arr(new int[10]); // 编译可以通过,但是运行时会出错
// 正确的做法是
std::unique_ptr<int[]> arr(new int[10]);
return 0;
}为什么 unique_ptr
unique_ptr
delete[]
unique_ptr
Zend框架2是一个开源框架,使用PHP 5.3 +开发web应用程序和服务。Zend框架2使用100%面向对象代码和利用大多数PHP 5.3的新特性,即名称空间、延迟静态绑定,lambda函数和闭包。 Zend框架2的组成结构是独一无二的;每个组件被设计与其他部件数的依赖关系。 ZF2遵循SOLID面向对象的设计原则。 这样的松耦合结构可以让开发人员使用他们想要的任何部件。我们称之为“松耦合”
344
使用
unique_ptr
unique_ptr
delete[]
unique_ptr
unique_ptr
unique_ptr
如何使用 std::make_unique
unique_ptr
C++14 引入了
std::make_unique
make_unique
new
new T[]
unique_ptr
虽然不能直接使用
make_unique<T[]>(size)
#include <iostream>
#include <memory>
template <typename T>
std::unique_ptr<T[]> make_unique_array(size_t size) {
return std::unique_ptr<T[]>(new T[size]);
}
int main() {
// 使用辅助函数创建 unique_ptr<int[]>
auto arr = make_unique_array<int>(5);
for (int i = 0; i < 5; ++i) {
arr[i] = i + 1;
std::cout << arr[i] << " ";
}
std::cout << std::endl;
return 0;
}unique_ptr
unique_ptr
unique_ptr
unique_ptr
std::move
#include <iostream>
#include <memory>
int main() {
std::unique_ptr<int[]> arr1(new int[5]);
for (int i = 0; i < 5; ++i) {
arr1[i] = i * 3;
}
// 转移所有权
std::unique_ptr<int[]> arr2 = std::move(arr1);
// 现在 arr1 不再拥有数组的所有权,访问 arr1 会导致未定义行为
// arr1[0] = 10; // 避免这样做
// arr2 现在拥有数组的所有权
for (int i = 0; i < 5; ++i) {
std::cout << arr2[i] << " ";
}
std::cout << std::endl;
// arr2 会在离开作用域时释放数组内存
return 0;
}如果需要共享资源,考虑使用
std::shared_ptr
shared_ptr
delete[]
delete
#include <iostream>
#include <memory>
int main() {
std::shared_ptr<int> arr(new int[5], [](int* p){ delete[] p; });
for (int i = 0; i < 5; ++i) {
arr.get()[i] = i * 5;
}
for (int i = 0; i < 5; ++i) {
std::cout << arr.get()[i] << " ";
}
std::cout << std::endl;
return 0;
}或者使用别名模板,使代码更清晰:
#include <iostream>
#include <memory>
template<typename T>
using shared_array = std::shared_ptr<T>;
int main() {
shared_array<int> arr(new int[5], std::default_delete<int[]>());
for (int i = 0; i < 5; ++i) {
arr.get()[i] = i * 5;
}
for (int i = 0; i < 5; ++i) {
std::cout << arr.get()[i] << " ";
}
std::cout << std::endl;
return 0;
}总结,
unique_ptr
unique_ptr<T[]>
shared_ptr
以上就是C++unique_ptr与数组结合使用方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号