Like Tree4Likes
  • 1 Post By anduril462
  • 1 Post By Juan Cervantes
  • 2 Post By anduril462

Segmentation Fault (core dumped)

This is a discussion on Segmentation Fault (core dumped) within the C Programming forums, part of the General Programming Boards category; Ok, I have been troubleshooting this with my teacher/tutor all day and I have it compiling but its giving a ...

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    5

    Question Segmentation Fault (core dumped)

    Ok, I have been troubleshooting this with my teacher/tutor all day and I have it compiling but its giving a segmentation fault. I used GDB to see the issue and its says its with strlen().

    If someone could point me in the right direction I would appreciate it. Not looking for the answer, I want to understand what I'm doing wrong. Thanks in advance

    Here is my code:
    Code:
    //    usage: ./gloriousStringLab [filename]
    //
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    
    #define MAX_STR 64
    
    
    void print_string(char *str);
    void write_string(char *str, FILE *fp2);
    
    
    int main(int argc, char *argv[]) {
        FILE *fp, *fp2;
        char *strings[MAX_STR];
        int string = 0;
        char temp[MAX_STR];
        int len, size = 0;
        int i, j;
        
        if(argc != 2) {
            printf("You're doing it wrong.\n");
            printf("usage: %s [filename]\n", argv[0]);
            return 1;
        }
    
    
        fp = fopen(argv[1], "r");
        if(fp == NULL) {
            printf("Unable to open file: %s\n", argv[1]);
            return 1;
        }
        
        
        
        //read each string from file
        while(fscanf(fp, "%s", temp) > 0){ 
            //malloc enough space in strings for string HINT STRLEN
            len = strlen(temp);
            strings[string] = malloc(len);
            //place string in strings HINT STRCPY
            strcpy(strings[string], temp);    
            memset(temp, '\0', len);
            string++;
        }
        string = string-1;
        fclose(fp);
        
        //print array to reverse
        for(i = string; string > 0; i--){
            printf("%s ", strings[i]);
        }
        printf("\n");
        
        //write to argv[1]reversed.txt USE write_str
        fp2 = fopen("reversed.txt", "w+");        //having issues here...
        for(j = 0; j < string; j++){            //having issues here...
            write_string(strings[j], fp2);
            j+=j;
        }
        fclose(fp2);
        
        //gg
        return 0;
    }
    
    
    void write_string(char *str, FILE *fp2) {
        fprintf(fp2, "%s ", str);
    }
    
    
    void print_string(char *str) {
        printf("%s ", str);
    }

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Providing a sample input file that exhibits this problem would be immensely helpful, I don't know how to format a file to test with or what data to put in there to cause the seg fault.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Actually, several problems I see:

    1. You only malloc len bytes. You need to malloc len+1 so you have room for the null character at the end of the string when you strcpy. You're probably trampling on memory that malloc uses internally to keep track of stuff.
    2. You don't check whether fopen succeeded in opening "reversed.txt"
    3. Your loop on line 54 doesn't have a proper termination condition.
    rags_to_riches likes this.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,649
    How distressing is this?

    I have been troubleshooting this with my teacher/tutor all day
    Who the heck is teaching these classes that can't see the issues anduril462 pointed out? Or doesn't know how to use GDB to debug a seg fault? How depressing.

  5. #5
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    the input file is just strings. I typed out a file with random words to fill the array.

  6. #6
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    He doesn't want to tell me just tells me it might be this, might be that...
    Median likes this.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Quote Originally Posted by Juan Cervantes View Post
    the input file is just strings. I typed out a file with random words to fill the array.
    Well, I did not encounter the seg fault that you had with strlen, so I need the exact input file you used, to help track down that problem. It may be related to one of the things I already mentioned, or there is something else I missed.

    There are many other possibilities for why I didn't get a seg fault in strlen, which include things like you and I using different implementations (I use Linux/GCC), sheer dumb luck (very common with bugs that result in undefined behavior like malloc/memory/seg fault issues), etc. But please provide that input file, to help narrow it down.

  8. #8
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    Thank you anduril462

  9. #9
    Registered User
    Join Date
    Oct 2012
    Posts
    5
    this is what I had in reversed.txt:

    hi bye here there new old old stall ball fall wall hello
    new blue glue dog boy cat baby girl baby love

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,444
    Did you use reversed.txt as your input file? That is, did you type "program_name reversed.txt" from the command line? Because reversed.txt is your output. I need to know what input caused the problems. If there was a different file you used for input, with different contents, please post that file.

    Oh, and I know you're new, but learn to read the full backtrace when debugging. When I get a seg fault, it happens in strlen, but it's not the strlen that you call directly from your program, it's called from other, printf-related functions in the standard C library, which are in turn called from a printf that you call in the code you wrote.
    Code:
    (gdb) backtrace
    #0  __strlen_ia32 () at ../sysdeps/i386/i686/multiarch/../../i586/strlen.S:56
    #1  0x001ad79f in _IO_vfprintf_internal (s=0x2c24e0, format=0x8048a7b "%s ", ap=0xbffff4f4 "\025")
        at vfprintf.c:1617
    #2  0x001b42c0 in __printf (format=0x8048a7b "%s ") at printf.c:35
    #3  0x0804886c in main (argc=2, argv=0xbffff734) at foo.c:55
    (gdb) frame 3
    #3  0x0804886c in main (argc=2, argv=0xbffff734) at foo.c:55
    55              printf("%s ", strings[i]);
    (gdb) print i
    $1 = -11
    • The "backtrace" command (or "bt" for short)
    • That line is where the seg fault actually happened.
    • That line is the place in your program that caused the seg fault. Sometimes (in a simple program like this), it is the bottom-most frame, sometimes not. You must know which functions you wrote (main) and which ones you didn't (__printf, _IO_vfprintf_internal and __strlen_ia32), to know which frame(s) to inspect.
    • The frame command takes you to the given frame number, so you can inspect all the local variables in that frame.
    • The print command shows you the value of variables or expressions based on the frame you're in.
    Salem and Juan Cervantes like this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault (core dumped)
    By KoRn KloWn in forum C Programming
    Replies: 3
    Last Post: 09-22-2012, 02:34 AM
  2. Segmentation Fault (core dumped)
    By Kevin Jerome in forum C++ Programming
    Replies: 5
    Last Post: 09-09-2012, 12:58 AM
  3. segmentation fault (core dumped)?
    By tkd_aj in forum C Programming
    Replies: 17
    Last Post: 04-18-2012, 01:53 AM
  4. Segmentation fault (core dumped)????
    By yosipoa in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2011, 01:18 PM
  5. Segmentation fault (core dumped)
    By JYSN in forum C Programming
    Replies: 1
    Last Post: 02-21-2002, 02:24 AM

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