Segmentation Fault

This is a discussion on Segmentation Fault within the C Programming forums, part of the General Programming Boards category; I get a segmentation fault when I run this program. I attempted debugging, and I think the problem is in ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    12

    Segmentation Fault

    I get a segmentation fault when I run this program. I attempted debugging, and I think the problem is in my pointer B, but I don't know why. The fault comes in the function "loesen" as soon as B is used

    Code:
    #include <stdio.h>
    int k=4;
    int eingabe(int i, int j, int n, int k, double **A, double *B){
         
         A = malloc(k * sizeof(**A));
         for(i=0; i<k; i++)
                    A[i]=malloc(k * sizeof(**A));
         printf("Geben Sie ein Oberematrix Reihweise ein\n");
         
         for(i=0; i<k; i++){
              for(j=i; j<k; j++){
                   scanf("%lf", &A[i][j]);
                   
              }
         }
         B = malloc(k * sizeof(*B));
         printf("Geben Sie Loesung Vektor ein\n");
         for(i=0; i<k; i++)
                  scanf("%lf", &B[i]);
         return 0;
                   
    }
    int ausgabe(int i, int k, double *X){
         for(i=0; i<k; i++)
                  printf("%lf", X[i]);
         return 0;
    }
    int loesen(int i, int j, int k, int n, double det, double *X, double *B, double **A, double summe){
         X = malloc(k * sizeof(*X));
         summe = 0;
         n = k-1;
         X[n] = B[n] / A[n][n];
         for(i=0; i<k; i++){
                  for(j=i+1; j<n; j++)
                             summe += A[i][j] * X[j];
                  X[i] = (1/A[i][i])* (B[i]-summe);        
         }
         for(i=0; i<k; i++)
                  det *= A[i][i];
         if(det==0)
                  printf("Matrix ist Singulaer\n");
                  
         return 0;
    }
    int main(){
        int i, j, n;
        double *X, *B, **A, summe, det;
        eingabe( i, j, n, k, A, B);
        loesen( i, j, k, n, det, X, B, A, summe);
        ausgabe( i, k, X);
        getch();
        return 0;
        
           
    }
    Last edited by lombardom; 06-05-2010 at 03:27 PM.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    Both A and B will remain unchanged after the return from eingabe since they are local variables inside eingabe.

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    12
    But I don't try to change A or B. I just want to access their storage locations to call up the values so that I can do calculations. I should still be able to access them, right?

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Damn, it's been too many times I encountered this.
    Read this.
    And think carefully.

    Code:
     
         A = malloc(k * sizeof(**A));
        //  you mean ?
         A = malloc(k * sizeof(*A) );

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    12
    ok, so I thought about it carefully and I would like to see if what I think so far is right. The problem is that my functions are only editing a copy of the pointer, so when I try to do use the values in the memory locations, I never actually allocated a value to them.

    In the link, the suggested solutions are as follows: either
    1) split my "eingabe" function into 2 functions and return the location of the pointers(&A and &B)
    or
    2) make a pointer point to the memory locations of my vectors/matrix

    Am I close?



    To the code, I have used what I typed previously and it worked, so I don't konw why it wouldn't this time.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    12
    apparently trying to return A and B doesn't work like I thought it would. I just don't understand the whole pointer thing as well as I should. I don't want to waste your time with trying to explain it to me step by step.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    If you want matrix, it's better to use struct.
    Code:
    typedef struct {
       int row,col;
       double **value;
    } matrix;
    If you still want prefer original approach,
    Code:
    int eingabe(int i, int j, int n, int k, double ***A, double **B){

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    You're not returning anything at all.
    Just because you have a * in there doesn't make it magically return things.

    Code:
    void foo ( int a ) {
      a = 1;
    }
    int main ( ) {
      int b = 0;
      foo( b );
      // what is b now?
    }
    To make it work, you need a pointer to the variable, from the callers perspective.
    Code:
    void foo ( int *a ) {
      *a = 1;
    }
    int main ( ) {
      int b = 0;
      foo( &b );
      // what is b now?
    }

    Now in your case, this would become
    Code:
    void foo ( double ***a ) {
      // well you get the idea by now ;)
    }
    int main ( ) {
      double **b = 0;
      foo( &b );
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,299
    Don't pass in variables that you don't read from before they are written to.
    Take the simplest function here:
    Code:
    int ausgabe(int i, int k, double *X){
         for(i=0; i<k; i++)
                  printf("%lf", X[i]);
         return 0;
    }
    The first things that happen with each variable are:
    'i' is set to 0
    'k' is read to compare it with 'i'
    'X' is read and used to access an array
    This indicates that 'i' is not meant to be a parameter to this function.

    Also, don't return something if you don't ever intend to do anything with that value (except in main).
    Lets correct these things:
    Code:
    void ausgabe(int k, double *X) {
        int i;
        for (i=0; i<k; i++)
            printf("%lf", X[i]);
    }
    Your other two functions have the same problems. Try fixing those the same way.
    Last edited by iMalc; 06-06-2010 at 03:21 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmentation fault... first time with unix...
    By theMethod in forum C Programming
    Replies: 16
    Last Post: 09-30-2008, 02:01 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 02:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21