Thread: getc Seg Fault

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    2

    Question getc Seg Fault

    Hi all,
    once again, its 3am, and I'm desperately trying to finish a C program in time.
    Basically, we have to write a program to demonstrate some hash tables etc... nothing too complicated (at least I expect this would be so had I gone to my lectures ).
    My problem is, that I seem to be getting a seg fault on windows -and- unix, upon getting to the getc function (or macro or whatever it is).

    The file seems to open correctly... (the actual code has error checking etc...)
    Code:
    data = fopen(data_file,"r"))
    I can also do fgets and read in entire lines... but when i try getc (or fgetc) i get a segmentation fault.
    Code:
        char c; // current character
        char currword[TMP_WORD_LEN]; // current word
        char *new_word; // Pointer to words that will be added to the array
        int i=0; // word index counter
        int wc=1; // word length counter
        while(c = getc(data))
    (note: 'data' is sent thru in the function header as (FILE *), however the same error occurs if i try to do the getc directly after opening the file)
    Prior to this, some other functions are called... one to initialise an empty hash table
    Code:
    void create_hash_table(int size)
    {
        printf("size: %d\r\n",size);
        hash_table = malloc(size*sizeof(struct hash));
        int i;
        for(i=0;i<=size;i++)
        {
            hash_table[i].contents = "NULL";
            hash_table[i].occurances = 0;
        }
        return;
    }
    ... and some basic arguement parsing...

    Thats... about it...
    O... except, that If i run in debugging mode (Dev-C++ 4.9.9.2), the program works perfectly (performs everything as expected)... unfortunately if it doesnt compile outside debugging... i get nothing ^^;;


    Any help would be greatly appreciated... in the mean time.. I might catch some shut eye (starting at the words "(c = getc(data))" for hours doesnt really help...)

    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    int c;
    while( (c = getc(data)) != EOF )
    > char *new_word;
    Where is this pointing?
    If you don't initialise it, then seg faults can't be far away.

    Seriously, there's not enough information in your post to determine why it crashes.
    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
    Registered User
    Join Date
    May 2005
    Posts
    2
    new_word is malloc'd later in the program...

    *edit*
    also, adding the !=EOF bit, doesnt help, and its only missing coz i check elsewhere (cant remember why XD )

    The reason I'm quite sure its getc, is that if i put a print statement immediately before, and immediately after
    eg:
    Code:
    printf("before\r\n");
    c = getc(data);
    printf("after\r\n");
    The first print statement will appear, then it will seg fault...
    That said, I'll attach a bit more of my code =S
    Code:
    int main(int argc, char *argv[])
    {
        create_hash_table(hash_total); // Set empty hash table
        hash_type(0);
        int verbose=0;
        FILE *data;
        char *data_file="NULL";
        FILE *search;
        char *search_file="NULL";
        char tmp_arg;
    .....
        if(strcmp(data_file,"NULL")==0)
        {
            printf("No input file specified...\r\n");
            return 0;
        }
        if((data = fopen(data_file,"r")) == NULL)
        {
            printf("Textfile failed to load...\r\n");
            return;
        }
        prepro_words(data);
    ......
    void prepro_words(FILE *data)
    {
        char c; // current character
        char currword[TMP_WORD_LEN]; // current word
        char *new_word; // Pointer to words that will be added to the array
        int i=0; // word index counter
        int wc=1; // word length counter
        while(c = getc(data))
        {
                if(i>=wc*TMP_WORD_LEN)
                {
                    int owc = wc++; // old word length counter
                    char tmp_word[owc*TMP_WORD_LEN];
                    strncpy(tmp_word,currword,owc*TMP_WORD_LEN-1);
                    free(currword);
                    char currword[wc*TMP_WORD_LEN];
                    strncpy (currword,tmp_word,owc*TMP_WORD_LEN-1);
                    free(tmp_word);
                }
                if(isalpha(c))
                    currword[i++] = (char) tolower((int) c);
    
    .... etc
    Last edited by Tyris; 05-25-2005 at 11:36 AM. Reason: update

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    adding the !=EOF bit, doesnt help
    That's because getc() returns an int, but you have c defined as a char. It will never match EOF as long as c is defined as a char.

    Also, why are you free()'ing non-malloc()'d memory?
    Code:
        char currword[TMP_WORD_LEN]; // current word
                    char tmp_word[owc*TMP_WORD_LEN];
                    free(currword);
                    char currword[wc*TMP_WORD_LEN];
                    free(tmp_word);
    You can't do this. You can only free() memory that's been allocated via malloc() and friends.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting a seg fault
    By ammochck21 in forum C Programming
    Replies: 11
    Last Post: 01-23-2009, 05:27 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. seg fault at vectornew
    By tytelizgal in forum C Programming
    Replies: 2
    Last Post: 10-25-2008, 01:22 PM
  4. weird seg fault
    By Vermelho in forum C Programming
    Replies: 3
    Last Post: 05-10-2008, 08:27 PM
  5. Seg Fault Problem
    By ChazWest in forum C++ Programming
    Replies: 2
    Last Post: 04-18-2002, 03:24 PM