5

我编写了一个简单的模板化 Matrix 类,用于处理数据矩阵的主应用程序。截断的矩阵代码是:

template <typename T> 
class Matrix{
   private:
      std::vector<T> matrixRepresentation;
      bool transposed;
  public: 
  Matrix(int r, int c);
  int maxRows;
  int maxCols;
  void setMatrixValue(int row, int col, T val);
  T getMatrixValue(int row, int col);
};

template <typename T>
Matrix<T>::Matrix(int r, int c){
   maxRows = r;
   maxCols = c;
   matrixRepresentation.resize((r+1)*(c+1));
}   

template <typename T>
void Matrix<T>::setMatrixValue(int row, int col, T val){
   matrixRepresentation[row + col*maxCols] = val;
}

template <typename T>
T Matrix<T>::getMatrixValue(int row, int col){
   return matrixRepresentation[row + col*maxCols];
}

如您所见,我只是将 2D 矩阵表示为向量并提供包装器方法来隐藏该事实。即使我将堆栈变量 matrixRepresentation 调整为

(r+1)(c+1)

我后来在代码中遇到了内存损坏问题,valgrind 告诉我以下内容:

==3753==    at 0x8049777: Matrix<int>::setMatrixValue(int, int, int) (in a.out)
==3753==    by 0x8049346: DataFile::readAllData() (ina.out)
==3753==    by 0x8049054: DataFile::DataFile(char const*) (in a.out)
==3753==    by 0x804C386: main (in a.out)
==3753==  Address 0x42cc970 is 0 bytes after a block of size 5,600 alloc'd
==3753==    at 0x4026351: operator new(unsigned int) (vg_replace_malloc.c:255)
==3753==    by 0x804A603: __gnu_cxx::new_allocator<int>::allocate(unsigned int, 
            void   const*) (in /a.out)
==3753==    by 0x8049F0D: std::_Vector_base<int, std::allocator<int> 
            >::_M_allocate(unsigned int) (in a.out)
==3753==    by 0x804A181: std::vector<int, std::allocator<int> 
            >::_M_fill_insert(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
            std::allocator<int> > >, unsigned int, int const&) (in a.out)
==3753==    by 0x8049AEF: std::vector<int, std::allocator<int> 
            >::insert(__gnu_cxx::__normal_iterator<int*, std::vector<int, 
            std::allocator<int> > >, unsigned int, int const&) (in a.out)
==3753==    by 0x80499AB: std::vector<int, std::allocator<int> >::resize(unsigned int,
            int) (in a.out)
==3753==    by 0x8049709: Matrix<int>::Matrix(int, int) (in a.out)
==3753==    by 0x80492AD: DataFile::readAllData() (in a.out)
==3753==    by 0x8049054: DataFile::DataFile(char const*) (in a.out)
==3753==    by 0x804C386: main (in a.out)

readAllData() 方法(此矩阵类的用户)只是从文本文件中读取并尝试填充矩阵

void DataFile::readAllData(){
   int currentValue;
   featureMatrix = new Matrix<int>((theHeader.totalNumSamples),
   (theHeader.numFeatures));

    if (infile.is_open()){
       if (!infile.eof()){
          for (int row=0; row < theHeader.totalNumSamples; row++){
            for (int col=0; col < theHeader.numFeatures; col++){
               infile >> currentValue;
               featureMatrix->setMatrixValue(row, col, currentValue);
             }
          }
       }
       else{
          cout << "EOF reached before we should have been done!  Closing file";
          infile.close();
       }
   }
   else cout << "File not open when attempting to read data";

   infile.close();
}

标头值是有效的(例如 theHeader.totalNumSamples = 15,theHeader.numFeatures = 100)。

如果我可以提供更多信息,请告诉我,我真的可以为此提供一些帮助。

4

3 回答 3

8

此代码段(在您的代码中出现两次):

 [row + col*maxCols]

是不对的。它应该是[row * maxCols + col][col*maxRows + row]

就此而言,您不需要matrixRepresentation.resize((r+1)*(c+1)); 分配,而是可以分配matrixRepresentation.resize(r*c);

您应该对极端情况进行数学计算(例如访问元素(maxRows-1,maxCols-1)以自行检查以进行理解)。

于 2012-04-24T23:40:49.307 回答
1

这能解决你的问题吗?

template <typename T>
Matrix<T>::Matrix(const int r, const int c) :
   maxRows(r),
   maxCols(c),
   matrixRepresentation((r+1)*(c+1))
{}

它可能会或可能不会解决它,但非常值得一试,如果只是为了排除Matrix.

更新:@ChrisA. 的答案更好。先试试那个。

于 2012-04-24T23:37:15.600 回答
1

不确定 Valgrind 的具体问题是什么,但我发现了这一点:

maxRows = r;
maxCols = c;
matrixRepresentation.resize((r+1)*(c+1));

然后,在您的构造函数中,您正在创建一个 3x3 矩阵,但分配了 16 个位置,而不是 9 个。虽然这不一定是问题,但您可能会通过放置此填充来隐藏其他错误。我的看法是只分配您需要的空间,如果您发现自己崩溃或表现不佳,请修复其中的问题,而不是在您的 Matrix 实现中补偿它们。

于 2012-04-25T00:00:21.533 回答