Thread: Re: Segmentation fault

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    4

    Re: Segmentation fault

    Hi all,

    I had recently met with segmentation fault in my program but i could not figure out why it had occured. Segmentation fault occurs when i am referencing to data that is not there ... So although my program could be compiled, it could not be ran due to segmentation fault ...

    Part of my program is as shown below...
    ================================================
    Code:
    typedef struct amino{
    char seq;
    struct amino *before;
    struct amino *after;        
    }AMINO;
    
    . 
    .
    .
    .
    int main (void)
    {FILE *data, *output, *output1;
     data = fopen ("Sample.txt", "r");
     output = fopen ("output_sample1.txt","w");
     output1 = fopen ("output_sample2.txt","w");
     char input[100] = {};
     AMINO *ptr;
     AMINO *start; 
     AMINO *start1;
     AMINO *next;
     AMINO *pop;
     AMINO *pop1;
    .
    . 
    .
    .
    if (start->seq == ' ' || start->seq == '\n')
                                  {   fprintf(output,"_");
                                      (AMINO*)pop = (AMINO*)start;
                                      (AMINO*)pop1 = (AMINO*)start->before;
                                       start = (AMINO*)pop1; 
                                       start->after = NULL;
                                       free(pop);
                                  }
    
    .
    .
    .
    .
    ================================================
    and the error starts at "start = (AMINO*)pop1;" ...
    could anone kindly enlighten me why ?

    Thanks so much!!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    start->seq

    start is just a pointer, where it is allocated?
    you cannot dereference pointer that points nowhere
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Well, since the problem is that you are not correctly malloc'ing memory somewhere, we need to see those mallocs.

    Incidentally, what system are you running this on? This kind of bug is really where using a debugger offers more help than the forums could.
    Callou collei we'll code the way
    Of prime numbers and pings!

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    Code:
    thanks vart and QuestionC... below is my complete prog ... thanks so much
    ===========================
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<strings.h>
    
    typedef struct amino{
    char seq;
    struct amino *before;
    struct amino *after;        
    }AMINO;
    
    int main (void)
    {FILE *data, *output, *output1;
     data = fopen ("Sample.txt", "r");
     output = fopen ("output_sample1.txt","w");
     output1 = fopen ("output_sample2.txt","w");
     char input[100] = {};
     AMINO *ptr;
     AMINO *start; 
     AMINO *start1;
     AMINO *next;
     AMINO *pop;
     AMINO *pop1;
     
     while (fgets(input,100,data)!= NULL)
           { if (strstr(input, "SQ   SEQUENCE   " ) != NULL)
                { fprintf (output, "=================================\n");
                  start = malloc (sizeof (AMINO));
                  start1 = start;
                  start->before = NULL;
                  start->after = NULL;
                      while ( (start->seq = fgetc (data)) != '/')
                          { fprintf (output, "%c", start->seq);
                              if (start->seq == ' ' || start->seq == '\n')
                                  {   fprintf(output,"_");
                                      (AMINO*)pop = (AMINO*)start;
                                      (AMINO*)pop1 = (AMINO*)start->before;
                                       start = (AMINO*)pop1;
                                       start->after = NULL;
                                       free(pop);
                                  }
                            next = malloc (sizeof(AMINO));
                            start->after = next;                       
                            next->before = start;
                            start = next;
                            start->after = NULL;
                          }
                   ptr = start1;
                   while(ptr->after != NULL)
                       {fprintf (output1, "%c", ptr->seq);
                        ptr = ptr->after;
                        }
                 fprintf(output1, "\n==============================\n");
                 free (start1);
               }
           }
    fclose (data);
    fclose (output);
    fclose (output1);
    return EXIT_SUCCESS; 
    }
    
    ============================

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    start->before = NULL;
    //some stuff
    pop1 = start->before; //so it is also NULL
    start = pop1;//and start also set to NULL
    start->after = NULL; //GPF
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    Sorry vart ... is the code supposed to point out the error or was it to correct the error ?
    I am so sorry ... just a C beginner ?
    And what is GPF?
    I think the struct AMINO has two pointers to itself so its before and after ...
    but in start->before there contains an address and i want to assign it to pop1 ... but by putting it to null, isn't it like removing the address it had ?
    Last edited by turkish_van; 01-18-2007 at 08:52 AM.

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by turkish_van
    Sorry vart ... is the code supposed to point out the error or was it to correct the error ?
    I am so sorry ... just a C beginner ?
    And what is GPF?
    I think the struct AMINO has two pointers to itself so its before and after ...
    but in start->before there contains an address and i want to assign it to pop1 ... but by putting it to null, isn't it like removing the address it had ?
    I just illustrated the error, showed what acctually your code did. I havn't put anything in the vars, just add comments to show what acctually was placed by you in each variable

    GPF - is General Protection Fault (segfault)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    4
    Oh my god vart ... you are so on the spot ... you actually pointed to something that i had neglected ...
    Thanks so much!!!!

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    #include<strings.h>
    I think you meant
    Code:
    #include<string.h>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 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, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM