1

i have this code trying to read a word from a list of words stored in a .txt file

char *getWord(char *filename)  {
    char formatstr[15], *word;
    static FILE *input;
    static int firstTime = 1;
    if (firstTime) { 
        input = fopen(filename, "r");
        if (input == NULL) {
            printf("ERROR: Could not open file \"%s\"\n", filename);
            exit(1);
        }
        firstTime = 0;
    }
    word = (char*)malloc(sizeof(char)*WORDLEN);
    if (word == NULL) {
        printf("ERROR: Memory allocation error in getWord\n");
        exit(1);
    }
    sprintf(formatstr, "%%%ds", WORDLEN-1);
    fscanf(input, formatstr, word);
    if (feof(input)) {
        fclose(input);
        return NULL;
    }   
    return word;    
}

And then this code to try and store it into a linked list

struct num1* wdStr(int wdLength, char *filename)
{

    int d; // total words.
    char *wordC;

    struct num1 *head = NULL;
    struct num1 *temp;  

    d = edCount(filename);      

    for(int i = 0; i < d; i++) 
    {       
        wordC = getWord(filename);              

        if(strlen(wordC) == wdLength)
        {
            temp = (struct num1*)malloc(sizeof(struct num1));
                temp->val = wordC;
                temp->next = head;
                head = temp;
        }       
    }   
    //return head;
}

edCount just counts how many words are there.

So after running the program, always before the last word i get a "Segmentation fault (core dumped)" error. I know i am doing something wrong with the pointers but still i cant find what.

ealiaj
  • 1,525
  • 1
  • 15
  • 25

2 Answers2

3

after running the program, always before the last word, I get a "Segmentation fault (core dumped)"

This is because your getWord returns NULL after the last word, but your main calls strlen on it unconditionally. This dereferences the NULL pointer, leading to a crash.

Note that your code produces a memory leak: your getWord implementation forgets to free the allocated word before returning NULL for the end of stream marker.

NOTE: Unless this is a specific learning exercise on statics, the use of FILE *input is highly questionable, because it makes your getWord non-reentrant for no good reason.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I know about the memory leak, but i just wanted to fix this issue first. Knowing that getWord gives NULL at eof i also tried to add this if(wordC ==NULL) return head; before if(strlen(wordC) == wdLength), but still i get the same error – ealiaj Nov 28 '13 at 12:25
  • @green_leaf Returning when `wordC` is `NULL` should have worked fine. The new crash is probably coming from some other place that calls your `wdStr` function. – Sergey Kalinichenko Nov 28 '13 at 12:33
  • indeed you where correct. And yes actually i have to use getWord and not alter it unfortunately, including the memory leak i suppose. – ealiaj Nov 28 '13 at 12:43
1

getWord returns NULL for eof case , you dont seem to be handling it !

Srikanth
  • 447
  • 2
  • 8