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

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    25

    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);
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You could also fix your indentation. It's a horrible mess.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Segmentation fault, core dumped
    By dweenigma in forum C Programming
    Replies: 2
    Last Post: 05-21-2007, 03:50 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Runtime error 'segmentation fault (core dumped)'
    By mikemccready in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:14 AM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 03:24 AM