memset で奇妙な問題が発生しています。これは、その前に作成しているクラスと、コンストラクターで開いているファイルに関係していました。私が扱っているクラスは通常、配列を読み取り、それを別の配列に変換しますが、それは重要ではありません。私が取り組んでいるクラスは次のとおりです。
#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;
};
他の関数は重要ではありません。なぜなら、私はそれらを呼び出すのをやめ、問題が解決しないからです。基本的に、コンストラクターに絞り込みました。
PreProcess::PreProcess(char* fileName,char* outFileName):newFileName(outFileName){
ifstream input(fileName);
input.close(); //this statement is inconsequential
}
コンストラクターでファイルも読み取りましたが、マトリックスを読み取らずにファイルを開くだけで問題が解決しないことがわかりました。基本的に、memset が適切に機能するこれらの 2 行をコメントアウトすると、それ以外の場合は機能しなくなります。
ここで、私が抱えている問題のコンテキストに移ります。マトリックス用の独自の単純なラッパー クラスを作成しました。機能はあまりありません。プロジェクトの次の部分で 2D 配列が必要なだけで、クラスですべてを処理する方が理にかなっています。
ヘッダー ファイル:
#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;
};
運転者:
#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;
}
私のメインプログラムは次のように実行されます:
PreProcess test1(argv[1],argv[2]);
//test1.SortedOrder();
Matrix test(10,10);
test.Display();
入力行のコメントを外してこれを実行すると、次のようになります。
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
memset を次のように置き換えた場合の補足として、これを引き起こすためにメモリ内で何が起こっているのか本当にわかりません。
for(int i=0;i<rows*columns;i++)
*(matrix+i) &= 0x0;
その後、完全に機能します。ファイルを開かない場合も機能します。それが役立つ場合、UbuntuでGCC 64ビットバージョン4.2.4を実行しています.memsetのいくつかの機能が正しく理解されていないと思います。