0
bool load(const char *dictionary)
{

FILE *dic=fopen(dictionary, "r");

node *hashtable[26];


int num=0;
char word[LENGTH+1];
while(fscanf(dic,"%s", word)!=EOF)
{
    node *new_node=malloc(sizeof(node));
   if (new_node==NULL)
   {
        unload();
        return false;
    }

    strcpy(new_node->word, word);
    int d=hash(word);
    if(hashtable[d]==NULL)
    {
        hashtable[d]=new_node;
    }
    else
    {
        new_node->next = hashtable[d];
        hashtable[d]= new_node;
    } 

    ++*num; 
}

fclose(dic);

if(*num==size())
{
    return true;
}
return false;
}

The debugger shows that the error occurs at ++*num. Why is that?

1

*num means "the variable the pointer num is pointing to". As num is 0, which is about the same as NULL, you dereference a null pointer. num per declaration as int is not meant as a pointer, but as a counter, and therefore only used as num, and never as *num.

Please note that malloc does not guarantee any value of the memory block allocated, especially the next pointer of a malloced node might initially point anywhere. Initialise it yourself, or use calloc instead of malloc, it zeroes the allocated space, but of course takes a bit longer for that operation.

Both your hash table and the number of elements in it probably should be variables defined outside the function instead, so you can access them in other functions as well.

BTW, if your hash function does not restrict output to 0..25, then place some %26 there (or better name some constant which is used both in declaration of the hash table and with the % operator, and also when iterating the hash table in the unload function)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .