次のようなコードを書きました。セグメンテーション違反エラーが発生します。デバッガーは、エラーが「some_order」から来ていることを示しています。ある特定の例について、変数の値を確認しました。n = 26 を取る: 次に、Var = {0,...,25} は、「some_order」に渡される u と v が範囲 (0-25) でなければならないことを意味しますが、7785654 のように、そのうちの 1 つに大きな値が得られます。または -1549259 (そのようなもの)。理由がわかりません。その場合、セグメンテーション違反は避けられません。
//TNT: template numeric toolkit
#include "tnt.h"
//contains includes to all files in http://math.nist.gov/tnt/tnt_doxygen/files.html
//all other necessary stl and standard c++ libaray includes are there
class global_data{
public:
static TNT::Matrix<double>* Value_matrix;
};
TNT::Matrix<double>* global_data::Value_matrix = NULL;
bool some_order(const int& u ,const int& v) {
return (*global_Data::Value_matrix)[v][u] == 0.0;
}
void some_function(int n){
std::vector<int> Var(n);
for(int i=0; i<n; i++){
Var[i] = i;
}
std::sort(Var.begin(), Var.end(), some_order );
}
int main(){
//assume we have n;
//nxn matrix, initialised with 0.0
global_data::Value_matrix = new TNT::Matrix<double>(n,n,0.0) ;
//global_data::Value_matrix is then filled with values
some_function(n);
delete[] global_data::Value_matrix
}