0

This block of code reads a dictionary file and stores it in a hashed array. This hashing array uses linked list collision resolution. But, for some incomprehensible reason, the reading stops in the middle. (i'm assuming some problem occurs when linked list is made.) Everything works fine when data is being stored in a empty hashed array element.

#define SIZE_OF_ARRAY 350
typedef struct {
    char* key;
    int status; // (+1) filled, (-1) deleted, 0 empty
    LIST* list;
}HASHED_ARRAY;
void insertDictionary (HASHED_ARRAY hashed_array[])
{
    //Local Declaration
    FILE* data;
    char word[30];
    char* pWord;
    int index;
    int length;
    int countWord = 0;

    //Statement
    if (!(data = fopen("dictionaryWords.txt", "r")))
    {
        printf("Error Opening File");
        exit(1);
    }

    SetStatusToNew (hashed_array); //initialize all status to 'empty'

    while(fscanf(data, "%s\n", word) != EOF)
    {

        length = strlen(word) + 1;
        index = hashing_function(word);

        if (hashed_array[index].status == 0)//empty
        {
            hashed_array[index].key = (char*) malloc(length * sizeof(char));//allocate word.
            if(!hashed_array[index].key)//check error
            {
                printf("\nMemory Leak\n");
                exit(1);
            }
            strcpy(hashed_array[index].key, word); //insert the data into hashed array.
            hashed_array[index].status = 1;//change hashed array node to filled.
        }

        else
        {
            //collision resolution (linked list)
            pWord = (char*) malloc(length * sizeof(char));
            strcpy (pWord, word);

            if (hashed_array[index].list == NULL) // <====== program doesn't enter
                                            //this if statement although the list is NULL. 
                                //So I'm assuming this is where the program stops reading.
            {
                hashed_array[index].list = createList(compare); 
            }
            addNode(hashed_array[index].list, pWord); 
        }
        countWord++;
        //memory allocation for key
    }
    printStatLinkedList(hashed_array, countWord);
    fclose(data);
    return;
}

createList and addNode are both ADT function. Former takes a function pointer (compare is a function that I build inside the main function) as a parameter, and latter takes list name, and void type data as parameters. compare sorts linked list. Please spot me the problem .

slow
  • 805
  • 3
  • 13
  • 27
  • Does it always stop at the same place when reading? Have you tried isolating the input involved? Have you tried stepping through the code in a debugger to see what happens? – Some programmer dude May 02 '13 at 05:52
  • it stops at the time when any word happens to have the same index as previous word, and then it goes through first else statement. And I'm not exactly sure how to use debugger. But is there any apparent error you can spot here? – slow May 02 '13 at 05:59

1 Answers1

1

Depending on where you declare the hashed_array you pass to this function, the contents of it may not be initialized. This means that all contents of all entries is random. This includes the list pointer.

You need to initialize this array properly first. The easiest way is to simple use memset:

memset(hashed_array, 0, sizeof(HASHED_ARRAY) * whatever_size_it_is);

This will set all members to zero, i.e. NULL for pointers.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621