如果下面的代码很长,很抱歉,但是我不知道与该问题有关的是什么,而与问题无关的是。
#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 */
当我在代码块中运行该程序时,没有错误,并且产生了所需的输出。但是,当我在大学的浏览器平台上使用它来编写程序时(类似于C ++的CodeSculptor),在所需的输出之后,它会出现以下错误:
***glibc detected*** double free or corruption (fasttop)
然后继续执行“内存映射”(这很长,所以我不确定是否应该发布它)。
我的问题是,如果代码有问题,那么当我在CodeBlocks中运行程序时为什么不显示?在这种情况下,代码有什么问题?另外,在浏览器平台上运行程序时,错误消息位于所需的输出之后。这是否意味着程序终止有问题?
感谢您的时间。
最佳答案
您没有实现the rule of three。
您没有提供复制分配运算符的这一行intArray2 = anArray;
将使用内置的该函数,它将盲目复制对象的每个成员。
现在,两个对象的T *arr
指向同一位置,并且在调用析构函数时..好了,您知道了... UB。 glibc确实是您的朋友。
这也导致了内存泄漏(以前的arr
指针被简单地覆盖了),但是与双重释放相比,这是一个小问题。