【问题标题】:Matrix pointer deallocation issue矩阵指针释放问题
【发布时间】:2014-06-08 01:52:45
【问题描述】:

我在运行以下程序时收到此错误(它完美地执行了代码,但看起来存在一些关于指针/内存的问题)。提前感谢您的帮助...

这是我收到的消息:

REVIEW(8310) malloc: * 对象 0x100103b80 的错误:未分配被释放的指针 * 在 malloc_error_break 中设置断点进行调试 (lldb)

我的代码:

在文件 Matrix.h 中

template <typename T>
class Matrix
{

   private:
T** M;
// unsigned numCol, numRow;
unsigned minRowIndex, maxRowIndex;
unsigned minColIndex, maxColIndex;




public:

Matrix();  // default constructor



Matrix(const unsigned& _numRow,
       const unsigned& _numCol,
       const T& value,
       const unsigned& _minRowIndex = 0,
       const unsigned& _minColIndex = 0);

~Matrix();


void Print();

T& operator() (const unsigned& row, const unsigned& col);

};

 template<typename T>
 Matrix<T> :: Matrix()

 {

     maxRowIndex = 9;
     maxColIndex = 9;

      unsigned i,j;

      M = new T*[maxRowIndex + 1];

     for (i =  minRowIndex; i < maxRowIndex; i++)
          M[i] = new T[maxColIndex + 1];

     for (i = minRowIndex; i < maxRowIndex; i++)
         for (j = minColIndex; j < maxColIndex; j++)
              M[i][j] = 0;

  }

   template<typename T>
    Matrix<T> :: Matrix( const unsigned& _numRow,
                 const unsigned& _numCol,
                 const T& value,
                 const unsigned& _minRowIndex,
                 const unsigned& _minColIndex)

   {

       minRowIndex = _minRowIndex;
       minColIndex = _minColIndex;
        maxRowIndex = _minRowIndex + _numRow - 1;
            maxColIndex = _minColIndex + _numRow - 1;

     unsigned i,j;

        M = new T*[maxRowIndex + 1];

      for (i =  minRowIndex; i <= maxRowIndex; i++)
          M[i] = new T[maxColIndex + 1];

      for (i = minRowIndex; i <= maxRowIndex; i++)
           for (j = minColIndex; j <= maxColIndex; j++)
               M[i][j] = value;
 }


 template<typename T>
 Matrix<T> :: ~Matrix()
 {
       for (unsigned i = 0; i <= maxRowIndex; i++)
           delete[] M[i];

        delete[] M;
 }


 template<typename T>
 void Matrix<T> :: Print()
 {
        unsigned i,j;

       for (i = minRowIndex ; i <= maxRowIndex; i++)
           for (j = minColIndex; j <= maxColIndex; j++)
       {
         cout << M[i][j] << "   ";

         if (j == maxColIndex)
            cout << endl;
       }

   cout << endl;cout << endl;

 }

   template<typename T>
   T& Matrix<T> :: operator() (const unsigned& row, const unsigned& col)
    {
          return  M[row][col];
    }

在 main.cpp 文件中

      #include "Matrix.h"


      using namespace std;

  int s(Matrix<int> L)
   {

      return L(2,2);
   }



 int main()
 {

     Matrix<int> L (5,5,100);
     cout << "L(2,2) = " << s(L) << endl << endl;

     return 0;    
 }

【问题讨论】:

  • 你需要自己做一些调试。找出问题发生的确切位置,并仅显示重现问题所需的代码。这只是一个代码转储。

标签: c++ pointers memory-management matrix


【解决方案1】:

使用newdelete 分配字段的类应该具有复制构造函数和重载operator=。否则,将使用简单复制字段的默认表单。

在您的情况下,T** M 在调用 s(L) 期间被复制,并且在返回时,副本被破坏,导致所有数组的删除。在 main 结束时,发生了另一次销毁,但现在指针指向已释放的块。

对于函数s,您还可以传递引用以避免复制(但这并不能解决一般问题)。

【讨论】:

  • 你能更详细地解释一下为什么我需要一个复制构造函数和重载 operator= 吗? (我明白会发生什么,但不明白为什么我们需要这个解决方案)。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-30
  • 1970-01-01
  • 2011-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多