A memory leak in std :: vector representing 2D data

I wrote a simple Matrix template class for use with my main application that manages data matrices. Truncated Matrix Code:

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];
}

As you can see, I simply represent the 2D matrix as a vector and provide wrapper methods to hide this fact. Even though I am resizing the matrixRepresentation matrix variable to

(r+1)(c+1)

In the end, I get memory corruption issues in the code, and valgrind tells me the following:

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

The readAllData () method (the user of this matrix class) simply reads from the text file and tries to fill the matrix

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();
}

The header values ​​are valid (for example, theHeader.totalNumSamples = 15, theHeader.numFeatures = 100).

, , , .

+5
3

( ):

 [row + col*maxCols]

. [row * maxCols + col] [col*maxRows + row]

matrixRepresentation.resize((r+1)*(c+1));, matrixRepresentation.resize(r*c);

(, (maxRows-1,maxCols-1), ).

+8

?

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

, , Matrix.

: @ChrisA. . .

+1

, Valgrind, :

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

In your constructor, you create, say, a 3x3 matrix, but allocate 16 places, not 9. Although this should not be a problem, you probably hide other errors with this add-on. My job was to highlight only the space that you need, and if you find yourself cursing or misbehaving, fix the problems, not compensate for them in the Matrix implementation.

+1
source

All Articles