0

trying to learn GSL (GNU Scientific Library)on mac OS X (El Capitan) following this tutorial (which is BTW the third result on my google search!).

  1. I installed GSL using homebrew
  2. following the comments on this post I changed flags from -lgslblasnative to -lgslcblas.

now it compiles but I get a Segmentation fault: 11 error while running the program. There are a number of possibilties. first is the stackoverflow, which is very unlikly with such a samll program. second there is an array somewhere and the program is trying to write on the memory which has not allocated. or there is something wrong with GSL. I would appreciate if you could help me.

edit 1: this is the code I'm trying to run

#include <stdio.h>
#include <gsl_rng.h>
#include <gsl_randist.h>

int main (int argc, char *argv[])
{
  /* set up GSL RNG */
  gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937);
  /* end of GSL setup */

  int i,n;
  double gauss,gamma;  

  n=atoi(argv[1]);
  for (i=0;i<n;i++)
  {
    gauss=gsl_ran_gaussian(r,2.0);
    gamma=gsl_ran_gamma(r,2.0,3.0);
    printf("%2.4f %2.4f\n", gauss,gamma);
  }
  return(0);
}
Community
  • 1
  • 1
Foad S. Farimani
  • 12,396
  • 15
  • 78
  • 193
  • Welcome to Stack Overflow. It would appear that you need to learn how to use a debugger to step line-by-line through your code, which will likely allow you to easily pinpoint the nature and location of the issue you're having. Using a debugger is, for all intents and purposes, required knowledge for any programmer. For more info, see [How to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Random Davis Nov 18 '16 at 21:32
  • actually I was just doing that! 1. installed gdb using brew 2. compile the code with -g flag 3. open the program inside gdb 4. run it entering r and here is the error I got: ` Program received signal SIGSEGV, Segmentation fault. 0x00007fff983a548b in strtol_l () from /usr/lib/system/libsystem_c.dylib ` – Foad S. Farimani Nov 18 '16 at 21:34
  • 2
    When you run the program, are you giving it a command-line argument? If not, `argv[1]` is NULL, and `atoi()` isn't going to like that. – Steve Summit Nov 18 '16 at 21:40
  • how am I supposed to do that? – Foad S. Farimani Nov 18 '16 at 22:05
  • With `myprogram 42` if you want `42` passed as a string. It essential to check `argc` to see if such argument was actually given. `if(argc < 2) return 1;` Please [see this](http://stackoverflow.com/a/5157549/4142924). – Weather Vane Nov 18 '16 at 22:48
  • @WeatherVane thanks a lot. I just realised that I didn't know what that argv is all about. as it was mentioned in the tutorial I should give an integer to the program when running. – Foad S. Farimani Nov 18 '16 at 23:21
  • Yes, but it arrives as string, the arguments you give are just text. – Weather Vane Nov 18 '16 at 23:23
  • that's why it uses atoi to change to an integer again! wondering if ti is possible to define it as integer. – Foad S. Farimani Nov 18 '16 at 23:26
  • No, the program arguments arrive as an array of string pointers. End of. – Weather Vane Nov 18 '16 at 23:33

0 Answers0