【发布时间】:2015-10-07 14:10:41
【问题描述】:
我正在尝试编写一个简单的程序代码,该代码通过 GSL 读取直方图,然后为我找到 x 轴上特定点的相应 bin 索引。代码如下所示:
#include <iostream>
#include <string>
#include <cmath>
#include <fstream>
#include <sstream>
#include <gsl/gsl_histogram.h>
int main() {
gsl_histogram* h_transform;
size_t h_Bins = 3;
h_transform = gsl_histogram_alloc(h_Bins);
double range[4] = { 1.0, 10.0, 100.0, 1000.0 };
double bins[3] = {7.0, 0.0011, 9e-02};
gsl_histogram_set_ranges(h_transform, range, 4);
for(int i=0; i<h_Bins; i++) {
h_transform->bin[i] = bins[i];
}
for (size_t i=0; i<h_Bins; i++) {
std::cout << "range[" << i << "] = " << h_transform->range[i] << std::endl;
std::cout << "bin[" << i << "] = " << h_transform->bin[i] << std::endl;
}
std::cout << "range[" << h_range_size << "] = " << h_transform->range[h_range_size] << std::endl;
size_t* h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, h_index);
std::cout << "after h_find" << std::endl;
std::cout << "h_index = " << *h_index << std::endl;
std::cout << "get = " << gsl_histogram_get(h_transform, *h_index) << std::endl;
return 0;
}
当我编译这段代码时
g++ -o gslTest gslTest.cpp -lgsl -lgslcblas -lm
并使用 ./gslTest 运行它,我得到以下输出:
range[0] = 1
bin[0] = 7
range[1] = 10
bin[1] = 0.0011
range[2] = 100
bin[2] = 0.09
range[3] = 1000
before find
Segmentation fault (core dumped)
现在我无法解决这个问题。几天前我遇到了同样的错误,修复了它,现在它又出现了,我不记得修复了......
希望你们中的一些人可能比我更擅长解决这个问题! 提前致谢!
【问题讨论】:
-
gsl_histogram_find的原型是什么?如果最后一个参数是 size_t 指针,那就有问题了。 -
原型是 int gsl_histogram_find (const gsl histogram * h , double x , size_t * i )
-
好吧,我的回答在您的代码中至少给出了一个不正确的地方。
-
是的,确实如此!非常感谢!
标签: c++ segmentation-fault histogram gsl