2016-10-11 11 views
0

ここでは、ビット行列を表すベクトルにゼロをロードするコードセグメントがあります。プログラムが実行され、出力ファイルに結果を書き込もうとすると、segフォルトが発生します。プログラムは、出力ファイルに書き込まれていないときに正常に動作します。出力ファイルに出力しますビット行列を使ったベクトルのロード

[code] 
Bitmatrix::Bitmatrix(int rows, int cols) 
{ 
    int count = 0;                   // count variable 
    int count2 = 0;                  // 2nd count variable 

    if(rows <= 0 || cols <= 0) 
    { 
     fprintf(stderr, "Value of rows or columns is less than or equal to zero\n"); // print error message 
     M.resize(1);                 // resize to 1 x 1 matrix 
     M[0] = '0';                 // set 0 as the value 
    } 
    else 
     M.resize(rows);               // resize matrix to number of rows 
     for(count = 0; count < M.size(); count++) 
     { 
      for(count2 = 0; count2 < cols; count2++) 
      { 
       M[count].push_back('0');          // fill matrix with zeros 
      } 
     } 
}[/code] 

機能は次のとおりです。なぜこれが起こっている

[code]void Bitmatrix::Write(string fn) 
{ 
    ofstream out;              // output stream object 
    int count = 0;             // count variable 
    int count2 = 0;             // 2nd count variable 

    out.open(fn.c_str());          // open output file 
    for(count = 0; count < M.size(); count++) 
    { 
     for(count2 = 0; count2 < M[count].size(); count++) 
     { 
      out << M[count][count2]; 
     } 
     out << endl; 
    } 
}[/code] 

誰でも見ることができますか?あなたがcount代わりのcount2をインクリメントしている2番目のforループでは、あなたの関数で

答えて

0

Write()

for(count2 = 0; count2 < M[count].size(); count++ <= should be count2 

あなたが外の行列に値をアクセスしているので、次の行が、その後、セグメンテーションフォールトを呼び出します境界。

+0

ありがとうございました。私はいつもそのようなことを見落としている。 – miamidawgs

+0

私の答えがあなたを助けたら、それを合格とマークしてください。 –

関連する問題