Search code examples
cgsl

gsl gnu scientific library segmentation fault when calling gsl_blas_ddot


When I compile the code below there are no errors reported by gcc :

#include <stdio.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>


int main (void)
{
  int i;

  gsl_vector * v = gsl_vector_alloc (3);

  for (i = 0; i < 3; i++)
    {
      gsl_vector_set (v, i, 1);
    }


  gsl_vector * v2 = gsl_vector_alloc (3);

  for (i = 0; i < 3; i++)
    {
      gsl_vector_set (v2, i, 2);
    }


  double *result ;

  gsl_blas_ddot(v, v2, result) ;


  printf("result of dot product is %f\n", *result );

  return 0;
}

But i get runtime error :

Segmentation fault (core dumped)

referred to the call to gsl_blas_ddot. I can't realize what is the problem. The v and v2 vectors are correctly allocated.


Solution

  • I'm not familiar with that library, however, the posted source code has this statement:

    double *result ;
    

    however, the pointer result is never set to point to any memory that the application owns.

    Suggest changing to :

    double result;
    

    and modifying the following line:

    gsl_blas_ddot(v, v2, result) ;
    

    to

    gsl_blas_ddot(v, v2, &result) ;