Thread: I don't know the cause of this segmentation fault

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    1

    I don't know the cause of this segmentation fault

    Hello. For one of my classes, I have to write a program that reads in a file that contains a string and looks for occurrences of substrings asked by the user. I am running this on Ubuntu by the way.

    Anyway, the terminal commands are

    echo "abcdefgh" > bar
    ./file bar bc

    where file is the name of my program, bar is the file that "abcdefgh" is saved to, and bc is the string the user wants to find. The code for my program is below

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <errno.h>
    
    
    int main(int argc, char *argv[])
    {
        FILE *pFile;
        char *textFile=NULL;
        textFile=(char *)malloc(sizeof(char)*10);
        pFile=fopen(argv[1],"r");
        
        int count;
        
        while(fgets(textFile,sizeof(textFile)-1,pFile)!=NULL)
        {
    
    
        if(argc>1)
        {
        for(count=1;((textFile=strstr(textFile,argv[count]))!=NULL);count++)
        {
        textFile++;
        }//end for
        }//end if
        }//end while
                
    printf("The string appeared: %d times\n",count);
    
    
    fclose(pFile);
    
    
    free(textFile);
    
    
    return 0;
    
    
    }//end main
    I am receiving the Segmentation Fault error but have no idea where the problem is or how to fix it. If someone could tell me, that would be great.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well there are so many places it could go wrong.

    > textFile=(char *)malloc(sizeof(char)*10);
    You don't check for success here.

    > pFile=fopen(argv[1],"r");
    You don't check for success here.

    > while(fgets(textFile,sizeof(textFile)-1,pFile)!=NULL)
    You're lying to fgets about the true size of the buffer. sizeof() in a pointer does NOT tell you how much data it is pointing to.

    > for(count=1;((textFile=strstr(textFile,argv[count]))!=NULL);count++)
    Your loop pays no attention to argc, so you could be accessing argv out of bounds

    > textFile++;
    Modifying a pointer to something malloc'ed

    > free(textFile);
    Freeing a modified pointer.
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One more Salem missed, the string that is being read in does not have a null-terminator appended.

    So yeah, there's 7 reasons why it would segfault all up so far. That's pretty impressive for a mere 41 lines of code.

    This is also a very very bad practice:
    Code:
        }//end for
        }//end if
        }//end while
    If you have to write what the curly bracket is for then you're doing it wrong.
    Learn about indentation and how each open curly bracket means the following code is tabbed out further and each closing curly bracket means that code is following tabbed out less. This is the first thing you need to get right when you start programming. It is not something to just get right later.
    Last edited by iMalc; 02-05-2012 at 01:44 AM.
    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"

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By browser in forum C Programming
    Replies: 8
    Last Post: 12-07-2010, 03:13 PM
  2. Segmentation fault?
    By nullifyed in forum C Programming
    Replies: 2
    Last Post: 05-30-2010, 08:10 PM
  3. Segmentation Fault
    By Scarvenger in forum C++ Programming
    Replies: 23
    Last Post: 10-20-2007, 06:45 AM
  4. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  5. segmentation fault and memory fault
    By Unregistered in forum C Programming
    Replies: 12
    Last Post: 04-02-2002, 11:09 PM