GSL Segmentation fault: 11 on mac - c

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!).
I installed GSL using homebrew
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);
}

Related

Segmentation fault on trying to execute value at environment variable

Hey so I was trying to solve a problem for beginners ctf event.
And this is the code that I am trying to run.
#include <stdio.h>
#include <stdlib.h>
int main(){
int (*func)();
func = getenv("MYENV");
func();
return 0;
}
I created a MYENV environment like this :
export MYENV=ls
but on running the code, it throws a segmentation fault (core dumped). I don't understand why.
The func function is basically calling the environment variable whose value is a simple command that I set. Why is it throwing an error.
I'm very new at linux and shell, so I'm sorry if this is too naive.
In C, if you want to run a system command, you have to use the system function (or one of the exec functions but that's more complicated):
#include <stdio.h>
#include <stdlib.h>
int main() {
char* cmd = getenv("MYENV");
system(cmd);
return 0;
}
If you're looking to run arbitrary code, you can inject shell code into it:
export MYENV=$'\x31\xc0\x50\x68\x2f\x2f\x73\x68\x68\x2f\x62\x69\x6e\x89\xe3\x50\x53\x89\xe1\xb0\x0b\xcd\x80'
You can learn more here.

Print statement not working on CodeBlocks for Mac

I've recently downlaoded code blocks for mac and for some reason my code compiles with no errors but when I try to run it in terminal to see if it prints it doesn't print anything. This is my code. I have already downloaded x code and my program is able to build in code blocks but print f will not work. Can someone please give a solution to this problem.
#include <stdio.h>
#include <stdlib.h>
int main()
{
printf("Hello world!\n");
return 0;
}

Unknown Segmentation Fault (Core Dumped) in multithread initialization

I've had three different lab TAs look at my code and none of them have been able to help me, so I've decided to try here. Unless I delete all code relating to both gettimeofday and any semaphores, I get a "Segmentation fault (core dumped)​" error. I've boiled down my code to only the main thread with simple declarations to attempt to get to the root of the problem.
My code:
#include <pthread.h>
#include <semaphore.h>
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <sys/shm.h>
#include <sys/time.h>
void *threadN (void *); /* thread routines */
pthread_t tid[1]; /* array of thread IDs */
int main()
{
int i,j;
/* here create and initialize all semaphores */
int mutex = sem_create(777777, 1);
int MatrixA[6000][3000];
for (i=0; i < 6000; i++) {
for (j=0; j < 3000; j++) {
MatrixA[i][j]=i*j;
}
}
int MatrixB[3000][1000];
for (i=0; i < 3000; i++) {
for (j=0; j < 1000; j++) {
MatrixB[i][j]=i*j;
}
}
int MatrixC[6000][1000];
struct timeval tim;
gettimeofday(&tim, NULL);
float t1=tim.tv_sec+(tim.tv_usec/1000000.0);
gettimeofday(&tim, NULL);
float t2=tim.tv_sec+(tim.tv_usec/1000000.0);
printf("%.2lf seconds elapsed\n", t2-t1);
sem_rm(sem_open(777777, 1));
return 0;
}​
I'm completely stumped here.
You eat your stack. See Radix sort for 10^6 array in C, comment from #Joachim Pileborg:
Local variables are usually stored on the stack, and the stack is usually limited to single-digit megabytes. On Windows for example, the default is 1MB per process, on Linux the default is 8MB...
I tried your code on Windows and it was dead with just MatrixA defined: 6000*3000*4 (for int)...
So you will have to move matrix data out of stack: define matrix as static or allocate on heap.
A useful thing I have found for tracking seg faults is with using gdb.
gcc -g -o a.out -c program.c
-g generates source level debug information
gdb a.out core
this starts up gdb with a.out
(gdb) run
this should run the program and show the line where the seg fault is happening.

Detecting a segmention fault in C

I have Win7 Pro (32 bit) and CodeBlocks IDE.
I would like to know is there any way to detect line with a segmention fault in C. My code is PRIME1.c
I find somewhere on Stack Overflow that this is possible on linux in terminal, but I would like to do that in Windows.
Could anyone tell me how to do that?
Many thanks!
In other words, I would like to know how to use debugger from cmd in windows 7 and how it can tell me which line is problematic.
I just found this link
Determine the line of C code that causes a segmentation fault?
But, as you can see, this is for Linux.
I would like to know how can I do that in Windows cmd?
You can catch seg fault. But, unfortunately, can not handle this event anyhow or get any info about that fault (in standard way, there are workarounds specific for compilers). So, maybe put printf in every line with __LINE__ macro and just wait until it fails.
#include <signal.h>
#include <conio.h>
#include <stdio.h>
void listener(int sig) {
printf("listener: access violation");
_getch();
}
void main() {
char a = 10;
char *p = &a;
signal(SIGSEGV, listener);
do {
printf("%d", *p++);
} while (1);
_getch();
}

C Build error when getting the value of sin()

I have recently started learning C as a side project. I am working under OpenSuse with the latest NetBeans using the GCC as toolset for compiling.
One of the very first programs that I made was this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
/*
*
*/
int main(int argc, char** argv) {
double rad = 1;
double result = 0;
result = sin(rad);
return (EXIT_SUCCESS);
}
This is a simple, no-brainer example that should have worked without a problem. However, I get a Build Error: Exit code 2(error in line 18, undefined reference to sin) when trying to compile.
Interestingly enough, if I remove the assignment of the value of sin(rad) to result OR replace rad with a hard coded value, the program compiles just fine.
What am I doing wrong here?
In C, you need to link to the math library:
Add this to the command line options:
-lm
Be sure that your are linking with the math library.
$ gcc myprog.c -lm

Resources