# Segmentation Fault (core dumped).. this is what it said when i compiled

Printable View

• 06-04-2010
rafacnsoccer
Segmentation Fault (core dumped).. this is what it said when i compiled
okay so i think i need to finish this and im done and like i said tis isnt my entire program
but can i get help with figuring whats wrong
im sure the error is here somewhere too

Code:

```void prob2(void) {  FILE *input_1;  input_1=fopen("inputf.dat", "r");  if(input_1==NULL) exit(1);    int m, q, i, j, n, k;  char line[64];  double **A, *x;  A=(double **)calloc(4,sizeof(double *));  for(i=0;i<4;i++)         A[i]=(double *)calloc(4 ,sizeof(double));  x=(double *)malloc(4*sizeof(double));    k=0;  while(fgets(line,64,input_1) != NULL)  {   if(k==4)     sscanf(line, "%lf %lf %lf %lf", &x[0],&x[1],&x[2],&x[3]);   else   {   sscanf(line, "%lf %lf %lf %lf", &A[m][0],&A[m][1],&A[m][2],&A[m][3]);   }   k++;    }   printf("Matrix A is:"); printf("\n\n"); for(q=0;q<4;q++)         printf("%6.3f ",A[0][q]); printf("\n"); for(q=0;q<4;q++)         printf("%6.3f ",A[1][q]); printf("\n"); for(q=0;q<4;q++)         printf("%6.3f ",A[2][q]); printf("\n"); for(q=0;q<4;q++)         printf("%6.3f ",A[3][q]); printf("\n\n"); printf("Vector x is:"); printf("\n\n"); printf(" x[] =  "); for(j=0;j<4;j++)         printf("%6.3f  ",x[j]); printf("\n\n"); n=4; double **A_s; A_s=(double **)calloc(4,sizeof(double *)); for(i=0;i<4;i++)         A_s[i]=(double *)calloc(4 ,sizeof(double)); A_s = smatrix (A,x,n); printf("Matrix A_s is:\n\n "); for(q=0;q<4;q++) printf("%6.3f ",A_s[0][q]); printf("\n"); for(q=0;q<4;q++) printf("%6.3f ",A_s[1][q]); printf("\n"); for(q=0;q<4;q++) printf("%6.3f ",A_s[2][q]); printf("\n"); for(q=0;q<4;q++) printf("%6.3f ",A_s[3][q]); printf("\n\n"); printf("Vector y is:"); printf("\n\n"); printf(" y[] =  "); double *y; y =(double *)malloc(n*sizeof(double)); y = dotp2(A_s,x,n); double norm; norm = norm2(x,n); for (i=0;i<n;i++)         y[i]=y[i]/norm; for(q=0;q<4;q++)         printf("%6.3f  ",y[q]); printf("\n\n"); free(A); free(A_s); free(x); free(y); }```
• 06-04-2010
quzah
Stop compiling as C++, compile as C, lose the casts to calloc, start checking the return values of all of your allocations and functions, to make sure you're actually getting what you expect to get. Start paying attention to what lines get printed before it segfaults, so you know where to start looking. Flinging variable declarations all around your program make it a b**** to follow, and your variable names aren't helping any either.

Quzah.
• 06-04-2010
Salem
Use the debugger.

Like
gdb ./myprog

Then type
run

Then type
bt
to find out where you are. Then use other commands to figure out how you got there.
• 06-05-2010
Elysia
You could also fix your indentation. It's a horrible mess.