当我修改、更改和“删除”原始指针时,是否需要指针副本
Is a pointer copy necessary when I modify, alter and `delete` the original pointer?
Rao (2012, p. 180, 清单 8.9( 说:"您在第 10 行创建副本的原因是为了让循环修改通过增量运算符 (++
( 使用的指针。new 返回的原始指针需要完整存储,用于第 26 行中的相应delete[]
需要使用 new 返回的地址调用,而不是任何随机值"。
0: #include <iostream>
1: using namespace std;
2:
3: int main()
4: {
5: cout << “How many integers you wish to enter? “;
6: int InputNums = 0;
7: cin >> InputNums;
8:
9: int* pNumbers = new int [InputNums]; // allocate requested integers
10: int* pCopy = pNumbers;
11:
12: cout<<“Successfully allocated memory for “<<InputNums<< “ integers”<<endl;
13: for(int Index = 0; Index < InputNums; ++Index)
14: {
15: cout << “Enter number “<< Index << “: “;
16: cin >> *(pNumbers + Index);
17: }
18:
19: cout << “Displaying all numbers input: “ << endl;
20: for(int Index = 0, int* pCopy = pNumbers; Index < InputNums; ++Index)
21: cout << *(pCopy++) << “ “;
22:
23: cout << endl;
24:
25: // done with using the pointer? release memory
26: delete[] pNumbers;
27:
28: return 0;
29: }
pCopy
真的有必要吗?我错过了什么?在下面的修改示例中,我不使用它,delete[]
似乎工作正常。
#include <iostream>
using namespace std;
int main()
{
cout << "How many integers do you want? " << endl;
int InputN = 0;
cin >> InputN;
int* pNumbers = new int [InputN];
cout << "allocated memory for " << InputN << " integers" << endl;
for (int Idx = 0; Idx < InputN; Idx++) {
cout << "enter number for index " << Idx << endl;
cin >> *(pNumbers + Idx);
}
cout << "Display all input numbers: " << endl;
for (int Idx = 0; Idx < InputN + 2; ++Idx) {
cout << "integer with index: " << Idx << " has value " << *(pNumbers + Idx) << " and pointer: " << pNumbers + Idx << endl;
//cout << pNumbers - 50000 << " points to " << *(pNumbers - 50000) << endl;
}
delete[] pNumbers;
cout << "after the delete: " << endl;
for (int Idx = 0; Idx < InputN + 2; ++Idx) {
cout << "integer with index: " << Idx << " has value " << *(pNumbers + Idx) << " and pointer: " << pNumbers + Idx << endl;
//cout << pNumbers - 50000 << " points to " << *(pNumbers - 50000) << endl;
}
return 0;
}
在以下情况下
{
type* ptr = new type[size]; // line A
/*
some code, for example
ptr++; // legal but dangerous/bad
*/
delete[] ptr; // line B
}
变量 ptr
的值,即指向的地址,在第 A
行和第 B
行中必须相同。否则,这是未定义的行为,并导致崩溃(如果幸运的话(。在第二个代码清单中,不使用指针变量来循环元素,而是使用索引。因此,原始指针从未更改,并且您满足了条件。
有几种方法可以确保满足此要求。
您可以将指针声明为
const
。{ type * const ptr = new type[size]; // not: type const*ptr // ptr++; // illegal: would give a compiler error delete[] ptr; }
您可以使用智能指针
{ std::unique_ptr<type[]> ptr = new type[size]; /* some code */ } // memory is automatically freed at end of scope of ptr
您可以使用容器,例如
std::vector
{ std::vector<type> vec(size); /* some code */ } // memory is automatically freed at end of scope of ptr
最后一个选项是最方便的,因为它还允许您更改分配的内存量,或在初始分配的内存之外添加元素(当矢量自动重新分配和扩展时(。
相关文章:
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 从堆栈分配的原始指针构造智能指针
- 将unique_ptr分配给原始指针
- 如何将唯一指针的 std::vector 转换为原始指针的 std::span?
- <Base> <Derived> 具有相同原始指针共享引用的 shared_ptr 和 shared_ptr 实例是否计数?
- C++原始指针和"delete"
- 为什么 C++ 地址中的矢量无法通过原始指针访问
- 复制后删除原始数组指针将前 3 个字节设置为 0
- 如何正确实现具有原始指针的类的复制构造函数?
- 如何在将原始指针移动到基类构造函数之前从unique_ptr中提取原始指针
- C++指针无法更改其原始值
- 为包含原始指针的对象C++智能指针
- unique_ptr<T>::get() 返回的指针在原始unique_ptr被销毁后不是 nullptr
- c++:复制、删除和运算符=在原始指针映射中
- C++模板,用于通过常量引用和原始指针传递向量
- C++为什么原始指针不会增加shared_ptr的引用计数?
- 从shared_ptr获取原始指针以将其传递给需要 raw 的函数
- 如何包装多级原始指针以赋予其容器语义
- 为什么我可以通过原始指针而不是shared_ptr来修改对象
- Qt - 将空指针(原始数据)转换为 QImage 并将其显示在标签上