c++ - Cast from void * produces Segmentation Violation error -


i'm using gnu scientific library implement module in program computes integrals numerically. functions based on example can found on gsl website in numerical integration examples:

and here's code (most of same in example):

typedef map<float, float> signaldata;  double f (double x, void * params) {       signaldata * alpha = static_cast<signaldata *>(params);       double f =  interpolatorgsl::interpolatedvaluefortime(alpha, x);       return f; }   float integrationcomparator::integral(signaldata * signaldata){        gsl_integration_workspace * w = gsl_integration_workspace_alloc (100000);        double result, error;        double expected = -4.0;        signaldata * alpha = signaldata;         gsl_function f;        f.function = &f;        f.params = &alpha;         gsl_integration_qags (&f, -3.36e-08, -2.36e-08 , 0, 1e-7, 100000,                              w, &result, &error);         printf ("result          = % .18f\n", result);        printf ("exact result    = % .18f\n", expected);        printf ("estimated error = % .18f\n", error);        printf ("actual error    = % .18f\n", result - expected);        printf ("intervals =  %d\n", w->size);         gsl_integration_workspace_free (w); 

}

the problem can tracked following line:

signaldata * alpha = static_cast<signaldata *>(params); 

the cast apparently not work correctly: if try signaldata object (that use method takes parameter, ie. method printing out) produces segmentation violation error (it prints out 4 random numbers before error). in code pasted above, it's interpolation method uses object , that's segmentation violations occurs:

interpolatorgsl::interpolatedvaluefortime(alpha, x); 

but due faulty casting well.

i don't have experience c++ , i've never used void pointers before, excuse me if stupid question, right way pass map<float, float> * void * parameter?

let's @ numerical integration example

   double f (double x, void * params) {    double alpha = *(double *) params;    double f = log(alpha*x) / sqrt(x);    return f;  }  ...    double alpha = 1.0;     gsl_function f;    f.function = &f;    f.params = &alpha; 

passed variable has type double * , casted double * in f function

in code

double f (double x, void * params) {   signaldata * alpha = static_cast<signaldata *>(params);   double f =  interpolatorgsl::interpolatedvaluefortime(alpha, x);   return f; } ...    signaldata * alpha = signaldata;     gsl_function f;    f.function = &f;    f.params = &alpha; 

you assign signaldata ** casting signaldata *

thus suggest remove 1 & symbol code following

f.params = alpha; 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -