views:

154

answers:

1

I'm having a weird problem with memset, that was something to do with a class I'm creating before it and a file I'm opening in the constructor. The class I'm working with normally reads in an array and transforms it into another array, but that's not important. The class I'm working with is:

#include <vector>
#include <algorithm>
using namespace std;
class PreProcess
{
 public:
  PreProcess(char* fileName,char* outFileName);
  void SortedOrder();
 private:
  vector< vector<double > > matrix;
  void SortRow(vector<double> &row);
  char* newFileName;
  vector< pair<double,int> > rowSorted;
};

The other functions aren't important, because I've stopped calling them and the problem persists. Essentially I've narrowed it down to my constructor:

PreProcess::PreProcess(char* fileName,char* outFileName):newFileName(outFileName){
  ifstream input(fileName);
  input.close(); //this statement is inconsequential
}

I also read in the file in my constructor, but I've found that the problem persists if I don't read in the matrix and just open the file. Essentially I've narrowed it down to if I comment out those two lines the memset works properly, otherwise it doesn't.

Now to the context of the problem I'm having with it: I wrote my own simple wrapper class for matrices. It doesn't have much functionality, I just need 2D arrays in the next part of my project and having a class handle everything makes more sense to me.

The header file:

#include <iostream>
using namespace std;
class Matrix{
 public:
  Matrix(int r,int c);
  int &operator()(int i,int j)
  {//I know I should check my bounds here
    return matrix[i*columns+j];
  }
  ~Matrix();
  const void Display();
 private:
  int *matrix;
  const int rows;
  const int columns;
};

Driver:

#include "Matrix.h"
#include <string>
using namespace std;
Matrix::Matrix(int r,int c):rows(r),columns(c)
{
  matrix=new int[rows*columns];
  memset(matrix,0,sizeof(matrix));
  }
const void Matrix::Display(){
  for(int i=0;i<rows;i++){
    for(int j=0;j<columns;j++)
      cout << (*this)(i,j) << " ";
    cout << endl;
  }
}
Matrix::~Matrix()
{
  delete matrix;
}

My main program runs:

PreProcess test1(argv[1],argv[2]);
//test1.SortedOrder();
Matrix test(10,10);
test.Display();

And when I run this with the input line uncommented I get:

0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 -1371727776 32698 -1 0 
0 0 0 0 6332656 0 -1 -1 0 0 
6332672 0 0 0 0 0 0 0 0 0 
0 0 0 0 -1371732704 32698 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 

I really don't have a clue what's going on in memory to cause this, on a side note if I replace memset with:

for(int i=0;i<rows*columns;i++)
   *(matrix+i) &= 0x0;

Then it works perfectly, it also works if I don't open the file. If it helps I'm running GCC 64-bit version 4.2.4 on Ubuntu.I assume there's some functionality of memset that I'm not properly understanding.

+2  A: 

You use memset() like this:

memset(matrix,0,sizeof(matrix));

Here matrix is a pointer, so sizeof(matrix) gives the size of a pointer, not the size of the array. To fill the whole array, use columns * rows * sizeof(int) instead.

sth
-_- Thank you, that's really obvious.
Jacob Schlather
A good compiler should also support `matrix=new int[rows*columns]();`
visitor