gsl_histogram_find 给出分段错误(c++、gsl)
gsl_histogram_find gives segmentation fault (c++, gsl)
我正在尝试编写一个简单的程序代码,它通过 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)
现在我无法解决这个问题。几天前我遇到了同样的错误,修复了它,现在它又出现了,我不记得修复了...
希望你们中的一些人可能比我更擅长解决这个问题!
提前致谢!
一个明显的问题是:
size_t* h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, h_index);
假设 gsl_histogram_find
最后一个参数是一个指针,那么您将向该函数发送一个未初始化的指针。 gsl_histogram_find
绝对无法对该指针做任何事情,除非检查它是否为 NULL,或者取消引用它,使用它,从而造成严重破坏。
你可能应该做的是:
size_t h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, &h_index);
当一个函数需要一个指针时,它几乎总是意味着该函数需要一个现有实体的地址。请参阅 this answer 关于将指针传递给函数及其真正含义的内容。
我正在尝试编写一个简单的程序代码,它通过 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)
现在我无法解决这个问题。几天前我遇到了同样的错误,修复了它,现在它又出现了,我不记得修复了...
希望你们中的一些人可能比我更擅长解决这个问题! 提前致谢!
一个明显的问题是:
size_t* h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, h_index);
假设 gsl_histogram_find
最后一个参数是一个指针,那么您将向该函数发送一个未初始化的指针。 gsl_histogram_find
绝对无法对该指针做任何事情,除非检查它是否为 NULL,或者取消引用它,使用它,从而造成严重破坏。
你可能应该做的是:
size_t h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, &h_index);
当一个函数需要一个指针时,它几乎总是意味着该函数需要一个现有实体的地址。请参阅 this answer 关于将指针传递给函数及其真正含义的内容。