【发布时间】:2013-07-24 09:34:37
【问题描述】:
我编写了如下代码。我收到分段错误错误。调试器显示错误来自“some_order”。我检查了一个特定示例的变量值。取 n = 26:然后, Var = {0,...,25} 这意味着传递给 'some_order' 的 u 和 v 必须在 (0-25) 范围内,但我得到其中一个的一些大值,例如 7785654或-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
}
【问题讨论】:
-
您的排序函数应该返回两个参数中的哪一个更大,但它似乎什么也没做。
-
您的
some_function没有任何可观察的结果。它创建一个向量并对其进行排序,然后将其丢弃。 -
@jcoder :这是基于 Value_matrix 值的不同排序
-
@TemplateRex 我没有发布整个代码,这是无关紧要的。下面还有更多代码。
-
我认为您没有发布部分代码,这对于找到解决问题的方法至关重要。