所需输出后出现 glibc 错误

glibc error after desired output

本文关键字:glibc 错误 输出      更新时间:2023-10-16

如果以下代码很长,我很抱歉,但我不知道什么与问题相关,什么不相关。

#include <iostream>
#include <cmath>
#include <cstdio>
using namespace std;
template <class T>
class Array;
template <typename T>
ostream& operator<< (ostream &output, Array<T> &obj);
template <class T>
class Array {
private:
int capacity;
public:
T *arr;
Array();
Array(int);
~Array();
void Erase();
T& operator[](int);
friend ostream& operator<< <>(ostream &output, Array &obj);
int GetLength();
};
/* Constructors and Destructors */
template <class T>
Array<T>::Array() {
capacity = 0;
arr = new T [capacity];
}
template <class T>
Array<T>::Array(int n) {
capacity = n;
arr = new T [capacity];
}
template <class T>
Array<T>::~Array() {
delete [] arr;
}
/* End of Constructors and Destructors */
/* Member Functions */
template <class T>
void Array<T>::Erase() {
delete [] arr;
capacity = 0;
arr = new T [capacity];
}
template <class T>
int Array<T>::GetLength() {
return capacity;
}
/* End of Member Functions */
/* Overloaded Operators */
template <class T>
T& Array<T>::operator[](int index) {
/*if (index < 0 || index >= capacity)
cout << "Index out of range!" << endl;
else */
return arr[index];
}
template <class T>
ostream& operator<<(ostream &output, Array<T> &obj) {
for (int i = 0; i < obj.capacity - 1; ++i)
output << "Array[" << i << "] = " << obj[i] << ", ";
output << "Array[" << obj.capacity - 1 << "] = " << obj[obj.capacity - 1];
return output;
}
/* End of Overloaded Operators */
/* Non-member Functions */
template <typename T>
void BubbleSort(Array<T> &t);
template <typename T>
T Sum(Array<T> &t);
template <typename T>
T Average(Array<T> &t);
template <typename T, typename M>
bool Equal(Array<T> &t, Array<M> &m);
template <typename T>
bool Equal(Array<T> &t, Array<double> &d);
/* End of Non-Member Functions */
/* Main Function */
int main()
{
int n;
double r;
cin>>r;
cin>>n;
Array<int> anArray(n);
Array<double> adArray(n);
Array<int> intArray2(n);
for (int nCount = 0; nCount < n; nCount++)
{
cin>>anArray[nCount];
adArray[nCount] = anArray[nCount] + r;
}
BubbleSort(anArray);
intArray2 = anArray;
cout<<"The arrays: "<<endl;
cout<<anArray;
cout<<endl<<"and "<<endl;
cout<<intArray2;
cout<<((Equal(anArray,intArray2))?" ARE":" ARE NOT")<<" same!"<<endl;
cout<<"The Average of the array adArray is: "<<Average(adArray)<<endl;
cout<<"The arrays: "<<endl;
cout<<anArray;
cout<<endl<<"and "<<endl;
cout<<adArray;
cout<<((Equal(anArray,adArray))?" ARE":" ARE NOT")<<" same!";
return 0;
}
/* End of Main */
/* Non-Member Function implementation */
template <typename T>
void BubbleSort(Array<T> &t) {
int j;
bool flag = true;
while (flag) {
flag = false;
for (j = 0; j < t.GetLength() - 1; ++j) {
if (t[j] > t[j + 1]) {
swap(t[j], t[j + 1]);
flag = true;
}
}
}
return;
}

template <typename T>
T Sum(Array<T> &t) {
T sum = 0;
for (int i = 0; i < t.GetLength(); ++i)
sum += t.arr[i];
return sum;
}
template <typename T>
T Average(Array<T> &t) {
return ( Sum(t)  / t.GetLength() );
}
template <typename T, typename M>
bool Equal(Array<T> &t, Array<M> &m) {
if ( t.GetLength() != m.GetLength() )
return false;
bool flag = true;
for (int i = 0; i < t.GetLength(); ++i) {
if ( t.arr[i] != m.arr[i] ) {
flag = false;
break;
}
}
return flag;
}
template <typename T>
bool Equal(Array<T> &t, Array<double> &d) {
if ( t.GetLength() != d.GetLength())
return false;
bool flag = true;
for (int i = 0; i < t.GetLength(); ++i) {
if ( abs(t.arr[i] - d.arr[i]) > 0.1 ) {
flag = false;
break;
}
if ( abs(Average(t) - Average(d)) > 0.5 ) {
flag = false;
break;
}
}
return flag;
}
/* End of Non-Member Function Implementation */

当我在代码块中运行此程序时,没有错误,并且会产生所需的输出。但是,当我在我们学院的浏览器平台上使用它来编写程序(类似于 CodeSculptor for C++)时,在所需的输出之后,它会给出以下错误:

***glibc detected*** double free or corruption (fasttop)

然后继续使用内存映射(它很长,所以我不确定我是否应该发布它)。

我的问题是,如果代码有问题,那么为什么当我在 CodeBlock 中运行程序时它没有显示?在这种情况下,代码有什么问题?此外,在浏览器平台中运行程序时,错误消息位于所需的输出之后。这是否意味着程序终止有问题?

谢谢你的时间。

你没有实施三法则。

由于您尚未提供复制赋值运算符,因此此行intArray2 = anArray;将使用内置运算符,该运算符将盲目复制对象的每个成员。

现在两个对象的T *arr指向同一个地方,当析构函数被调用时......好吧,你明白了......噗。格利布克真的是你的朋友。

它还导致了内存泄漏(前一个arr指针被简单地覆盖了),但与双重释放相比,这是一个小问题。