期望输出后的glibc错误

时间:2015-05-15 17:29:40

标签: c++ glibc

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

#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)

然后继续使用记忆图(它很长,所以我不确定是否应该发布)。

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

感谢您的时间。

1 个答案:

答案 0 :(得分:2)

您没有实施the rule of three

由于您没有提供复制赋值运算符,因此intArray2 = anArray;这一行将使用内置函数,它会盲目地复制对象的每个成员。

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

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