简体   繁体   中英

gsl_histogram_find gives segmentation fault (c++, gsl)

I'm trying to write a simple program code which reads in a histogram via GSL and then finds me the corresponding bin index for a specific point on the x-axis. The code looks like this:

#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;
}

When I then compile this code with

g++ -o gslTest gslTest.cpp -lgsl -lgslcblas -lm

and run it with ./gslTest I get the following output:

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)

Right now I can't wrap my head around this. I got the same error a few days ago, fixed it and now it reappeared and I can't remember the fix...

Hopefully, some of you might be better at solving this than me! Thanks in advance!

One glaring issue is this:

size_t* h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, h_index);

Assuming that gsl_histogram_find last parameter is a pointer, then you are sending an uninitialized pointer to that function. There is absolutely nothing gsl_histogram_find can do with that pointer except check if it is NULL, or dereference it, use it, and thus cause havoc.

What you probably should be doing is this:

size_t h_index;
double x = 1.1;
std::cout << "before find" << std::endl;
gsl_histogram_find(h_transform, x, &h_index);

When a function wants a pointer, it almost always means that the function wants the address of an existing entity. Please see this answer with respect to passing pointers to functions and what it really means.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM