Thread: First debugging - Segmentation fault

  1. #1
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37

    First debugging - Segmentation fault

    Hi dear friends,
    I have problem including Segmentation fault and I don't know how to repair it. I am using gdb but I haven't knowledge how to repair mistake like this:
    Code:
    Program received signal SIGSEGV, Segmentation fault.
    0xb7e66021 in _IO_vfscanf_internal (s=s@entry=0x804b008, format=format@entry=0x8048d89 "%s", argptr=argptr@entry=0xbffca388 " ", errp=errp@entry=0x0) at vfscanf.c:1095
    1095    vfscanf.c: No such file or directory.
    (gdb)
    I think so this mistake means so I filled much memory but I don't know, how could I make it correctly. Can you help me please? Should I share my code?

    Thank you, greetings Tomas.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Use "bt" (backtrace) to find out where, in your code, the problem happened.

    It looks like you're passing an invalid value to scanf() (or related function), though. Hard to say without seeing code.

  3. #3
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    It gave me this:
    Code:
    #0  0xb7e66021 in _IO_vfscanf_internal  (s=s@entry=0x804b008, format=format@entry=0x8048d89 "%s",  argptr=argptr@entry=0xbffca388 " ", errp=errp@entry=0x0) at  vfscanf.c:1095
    #1  0xb7e6d607 in __isoc99_fscanf (stream=0x804b008, format=0x8048d89 "%s") at isoc99_fscanf.c:34
    #2  0x08048906 in main () at main.c:65

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by maestorm View Post
    It gave me this:
    Code:
    #0  0xb7e66021 in _IO_vfscanf_internal  (s=s@entry=0x804b008, format=format@entry=0x8048d89 "%s",  argptr=argptr@entry=0xbffca388 " ", errp=errp@entry=0x0) at  vfscanf.c:1095
    #1  0xb7e6d607 in __isoc99_fscanf (stream=0x804b008, format=0x8048d89 "%s") at isoc99_fscanf.c:34
    #2  0x08048906 in main () at main.c:65
    Please show us main.c. The problem seems to be related to line 65 of that file. Need to see that line and any related variable definitions.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    Here is part of code and thank you for your help, really thank you.
    Code:
     do
     60     {
     61         c = fgetc(fr);
     62         if(isalpha(c))
     63         {
     64                 fseek(fr, -1, SEEK_CUR);        //zjisti ze je tam alfabet. znak, posune se zpet a nacte slovo
     65                 fscanf(fr, "%s", s[cisloSlova].cele);
     66                 upravSlovo(s[cisloSlova].cele);
     67                 int length = strlen(s[cisloSlova].cele);
     68                 if(length>5) {
     69                     //if(CompFunc(s[cisloSlova].cele) == true) {
     70                         fprintf(fh, "%d " , cisloSlova);
     71                         fprintf(fh, "%s\n" , s[cisloSlova].cele);
     72                         //printf("\n%d: %s", cisloSlova, s[cisloSlova].cele);
     73 
     74                     //}
     75                 }
     76                 cisloSlova++;
     77         }
     78 
     79     }while(feof(fr)==0);
     80 
     81     fclose(fr);
     82     fclose(fh);

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > fscanf(fr, "%s", s[cisloSlova].cele);
    How is s declared?
    What is the current value of cisloSlova? (ask the debugger)
    How is the cele member declared?
    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.

  7. #7
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    s is declared: Slovo s[6000];
    int cisloSlova = 0; //initialization and after it is incremented for every word
    char cele[21]; //in structure on begin

  8. #8
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Quote Originally Posted by maestorm View Post
    s is declared: Slovo s[6000];
    int cisloSlova = 0; //initialization and after it is incremented for every word
    char cele[21]; //in structure on begin
    What is the structure of struct Slovo? How are the members defined?

  9. #9
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    Here is whole code, but still some parts I am working on..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <string.h>
    #include <stdbool.h>
    
    typedef struct
    {
        char cele[21];
        int pocet;
        char a;
        char b;
        int length;
    }Slovo;
    
    void upravSlovo(char*);
    void vstupString(char*);
    //bool CompFunc(char*);
    
    int main(void) {
    
        Slovo s[6000];
        int cisloSlova = 0;
    
        char c;
    
    
        printf("\nPress any key");
        getchar();
    
        FILE *fr;
        fr = fopen("file.txt", "r");
    
        FILE *fh;
        fh = fopen("helper.txt", "a+w");
    
        int celkovaDelka = 0;
        do
        {
            c = fgetc(fr);
            celkovaDelka++;
        }while(feof(fr)==0);
    
        char *obsah;
        obsah = (char*)malloc(celkovaDelka);
    
        rewind(fr);
    
        int i = 0;
        do
        {
            c = fgetc(fr);
            obsah[i] = c;
            i++;
        }while(feof(fr)==0);
    
        rewind(fr);
        
        do
        {
            c = fgetc(fr);
            if(isalpha(c))
            {
                    fseek(fr, -1, SEEK_CUR);        //zjisti ze je tam alfabet. znak, posune se zpet a nacte slovo
                    fscanf(fr, "%s", s[cisloSlova].cele);
                    upravSlovo(s[cisloSlova].cele);
                    int length = strlen(s[cisloSlova].cele);
                    if(length>5) {
                        //if(CompFunc(s[cisloSlova].cele) == true) {
                            fprintf(fh, "%d " , cisloSlova);
                            fprintf(fh, "%s\n" , s[cisloSlova].cele);
                            //printf("\n%d: %s", cisloSlova, s[cisloSlova].cele);
    
                        //}
                    }
                    cisloSlova++;
            }
    
        }while(feof(fr)==0);
    
         fclose(fr);
        fclose(fh);
    
        int pocetSlov = cisloSlova;
    
        int o;
        for(o=0;o<pocetSlov-1;o++) s[o].pocet = 1;
        for(o=0;o<pocetSlov-1;o++)
        {    
            for(i=pocetSlov-1;i>0;i--)
            {
                if(s[o].cele!=s[i].cele)
                {
                    if(strcmp(s[o].cele, s[i].cele)==0)
                    {
                        s[o].pocet++;
                    }
                }
    
            }
            printf("\n %s: %d",s[o].cele , s[o].pocet);
        }
        Slovo max[10];
        max[0].pocet = 1;
        int maximum = 1;
        i = 0;
    
        char *maximalniSlovo;
    
    
            for(o=0;o<pocetSlov-1;o++)
            {
                if(s[o].pocet>maximum)
                {
                    maximum = s[o].pocet;
                    maximalniSlovo = s[o].cele;
                    max[0].pocet = maximum;
                }
            }
    
    
            printf("\n Slovo s maximalnim vyskytem %d - %s", max[0].pocet, maximalniSlovo);
    
        free(obsah);
        return 0;
    }
    
    void upravSlovo(char *verb)
    {
        bool upraveno = false;
        do
        {
            if(isalpha(verb[strlen(verb)-1]))
            {
                upraveno = true;
            }
            else
            {
                verb[strlen(verb)-1] = '\0';
            }
        }while(upraveno==false);
    
    
    }
    
    void vstupString(char *verb)
    {
    
        if(fgets(verb, 20, stdin)!=NULL)
        {
            char *p = strchr(verb, '\n');
            if(p!=NULL) *p = '\0';
        }
    
    }
    
    /*bool CompFunc(char *verb) { // Použít rekurzi
    int i = 0;
    char array[i];
    for(;i<=2;i++) {
        
    }
    return bool;
    }
    */

  10. #10
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,111
    Here is whole code, but still some parts I am working on..
    You would also need to provide the two files specified in the code. It may or may not have an effect on the seg fault, but we should have the whole package.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So why haven't you used the debugger yet?

    You've caught a segfault, but then what?

    Type
    frame 2
    to get to main - presumably line 65 still.

    Type
    print cisloSlova
    to see if you're out of bounds of your array.
    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.

  12. #12
    Registered User
    Join Date
    Oct 2012
    Location
    Svitavy, Czech Republic
    Posts
    37
    It gave me this:
    Code:
    (gdb) run
    Starting program: /home/maestorm/fi.muni/C_programming/files/dutomas/a.out 
    
    Press any key
    
    Program received signal SIGSEGV, Segmentation fault.
    0xb7e66021 in _IO_vfscanf_internal (s=s@entry=0x804b008, format=format@entry=0x8048d89 "%s", argptr=argptr@entry=0xbffca388 " ", errp=errp@entry=0x0) at vfscanf.c:1095
    1095    vfscanf.c: No such file or directory.
    (gdb) frame 2
    #2  0x08048906 in main () at main.c:65
    65                    fscanf(fr, "%s", s[cisloSlova].cele);
    (gdb) print cisloSlova
    $1 = 6108
    (gdb)

  13. #13
    Registered User
    Join Date
    Jun 2014
    Posts
    79
    Maybe this is not the source of your problem, but I noticed that your didn't check for NULL pointers when opening files, nor when allocating dynamic memory. Just trying to guess, here... maybe you are making use of any NULL pointers when calling any file related standard functions? Or maybe you are using any NULL pointers supposed to point to any non-existent space in dynamic memory?

    Code:
    FILE *fr;
    fr = fopen("file.txt", "r");
    /* if (fr == NULL ) { printf("Unavailable file."); return 0; } */
    
    FILE *fh;
    fh = fopen("helper.txt", "a+w");
    /* if (fh == NULL ) { printf("Unavailable file."); return 0; } */
    
    /* skipped portions of your code */
    
    char *obsah;
    obsah = (char*)malloc(celkovaDelka);
    /* if (obsah == NULL ) { printf("Unable to allocate memory."); return 0; }  */
    Last edited by aldo_baldo; 12-30-2014 at 04:46 AM.

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by maestorm View Post
    Code:
    (gdb) print cisloSlova
    $1 = 6108
    (gdb)
    There is the cause of the crash. Your code is modifying element 6108 of an array with 6000 elements.

    Now you need to work out why that variable is getting a value of 6108.

    Two obvious things you need to look at.

    1) Check all operations on the file (fseek(), reading, etc) to see if they succeed. Your code doesn't check, but then proceeds as if every read succeeds. That is dangerous.
    2) Your upravSlovo() function will use negative indices to access array elements if supplied input with no alphabetic characters - which is possible if a read operation fails (given that your code proceeds anyway). That gives undefined behaviour.

    As a general comment, it is a really bad idea to mix usage of fseek() and fscanf() on the same file.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In GDB no segmentation fault but while running segmentation fault
    By Tamim Ad Dari in forum C++ Programming
    Replies: 2
    Last Post: 12-10-2013, 11:16 AM
  2. Segmentation Fault, could not find after debugging
    By Campocalypse in forum C Programming
    Replies: 9
    Last Post: 04-15-2013, 11:53 PM
  3. debugging segmentation fault
    By -EquinoX- in forum C Programming
    Replies: 4
    Last Post: 10-26-2008, 12:14 PM
  4. debugging: sigsegv, segmentation fault???
    By Gonzo in forum C Programming
    Replies: 9
    Last Post: 09-16-2003, 06:56 AM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM

Tags for this Thread