c++ - 数组索引重载错误
问题描述
为什么这段代码会导致数组的第二个元素被打印为 0,而不管整数数组对象的定义中指定的值是什么?下面代码的输出是 7 0 3 4 5 6 而不是 7 2 3 4 5 6,这种行为的原因是什么?
// Overloading operators for Array class
#include<iostream>
#include<cstdlib>
using namespace std;
// A class to represent an integer array
class Array
{
private:
int *ptr;
int size;
public:
Array(int *, int);
// Overloading [] operator to access elements in array style
int &operator[] (int);
// Utility function to print contents
void print() const;
};
// Implementation of [] operator. This function must return a
// reference as array element can be put on left side
int &Array::operator[](int index)
{
if (index >= size)
{
cout << "Array index out of bound, exiting";
exit(0);
}
return ptr[index];
}
// constructor for array class
Array::Array(int *p = NULL, int s = 0)
{
size = s;
ptr = NULL;
if (s != 0)
{
ptr = new int[s];
for (int i = 0; i < s; i++)
ptr[i] = p[i];
delete ptr;
}
}
void Array::print() const
{
for(int i = 0; i < size; i++)
cout<<ptr[i]<<" ";
cout<<endl;
}
// Driver program to test above methods
int main()
{
int a[] = {1, 2, 3, 4, 5, 6};
Array arr1(a, 6);
arr1[0] = 7;
arr1.print();
arr1[8] = 6;
return 0;
}
解决方案
在Array
构造函数中,在分配和填充动态分配ptr
的缓冲区后立即释放缓冲区
delete ptr;
在此之后对缓冲区的所有访问都会ptr
调用未定义的行为。旁注:这应该是delete[] ptr;
为了确保正确释放阵列。
解决方案:不要那样做!
ptr
当Array
超出范围并使用缓冲区完成时,添加一个析构函数以释放。
// destructor for array class
Array::~Array()
{
delete[] ptr;
}
delete[]
编译器会自动为您生成一个析构函数,但该泛型析构函数没有资格知道它对指针成员的内容是否安全。它可能不是一个数组,分配可能由另一个对象拥有(参见什么是资源或指针的所有权?)或者可能不是动态分配的new
。
这带来了一个旁注:处理复制此对象的默认特殊成员函数将盲目地复制指针,而不是分配,并留下两个指向同一分配的对象。迟早这将是致命的,因为一个副本将在另一个之前超出范围,如果没有其他尝试访问释放的分配并破坏程序,第二个delete[]
将破坏程序。什么是三法则?
一般的经验法则是不要创建这样的类,而是使用std::vector
. std::vector
完成所有这些以及更多。
推荐阅读
- c# - 如何检测 Unity2D 屏幕左下 20% 的触摸?
- android - 如何设置状态栏背景颜色
- python - Flask sqlalchemy 在没有元组结果的查询期间添加列
- c - 为什么我们在使用 scanf 函数时需要指定变量的地址而不是变量的名称
- javascript - 如何使用脚本在谷歌工作表中获取“双甜甜圈”图表
- javascript - 为什么对象原型属性不会覆盖给定代码
- java - 如何在 openshift 中部署 Spring Boot SSL 项目
- asp.net - 如何
和 一起工作? - angular - 如何在 Angular 9 中添加消耗性侧边栏
- scala - 在 Scala 中,解析 CSV 并仅将匹配的行添加到 List 中