0

I have been trying to figure Speller on my own but have been unable to.

My first problem in a list of never ending ones is this -

segmentation fault over this line of code

while (fscanf(dict, "%s", dictword) != EOF)

where dictword is initialised as such

char *dictword = "ha";

Now this initialisation to "ha" is a story of irony on its own.

I had to initialise it because later on in the code, the function line fscanf() needed the variable to be initialised to some value before accepting it as an input. But, the value could not be NULL as the function strcasecomp threw an error stating that the argurment needed the variable to not be initialised to NULL(even though the value clearly gets updated once I run the fscanf function over it)

I even tried to initialise dictword as char dictword[LENGTH] but that was another surprise as the code suddenly went topsy turvy and returned a runtime error (along with the segmentation fault) with the following text

runtime error: member access within null pointer of type 'node' (aka 'struct _node')

which was on an entirely different line of code.

name this -> hashtable[index] -> next = new_node;

Tbh, now I am slowly going crazy with this pset. Could really use some help. Here's the code for entire dictionary.c which might throw some light on my ailing problems.

Full Code

Edit - The code works now thanks to the answer. The gist is now updated with the working code. You may view the revisions to look for the older version.

1

"ha" is a string constant. The compiler stores the three bytes {'h','a','\0'} in a read-only area and initialises dictword to a pointer to this memory. When fscanf writes there, it segfaults.

You could instead use like char dictword[LENGTH+1];, which provides enough space for a word of LENGTH characters plus the null terminator.

memset before testing the pointer for NULL might be a bad idea, for about the same reason. Also, '0' is not the same as '\0' or equivalently just 0, which you probably meant. And there's calloc which is malloc with zeroing the memory, if you really need to, but guess what, you don't, as you write values to both fields.

hashtable[index] -> next makes no sense to me (in case hashtable[index] is NULL, which it initially is, what would it be?), you probably meant hashtable[index] instead. No need to treat hashtable[index] == NULL in any special way.

And your hash function returns different hashes for upper- and lower-case, but your code assumes it doesn't.

9
  • Thanks a lot for this, it really cleared many doubts. The hash_function is really a pain. I tried to covery every string passed to is as a lower case one using tolower but even after that, it returns a segfault in the search function as it magically returns -175 as a search index. We were advised to look online for hashfunctions, and I did find this one on the web(no source, I got it from a forum). But really, I was at dearth of sources to find some quality hashfunctions hence I stuck to this one. Apr 4 '18 at 15:34
  • To be sure, after updating the code, the dictionary loads just fine, it's during lookup that the it shows a segfault. Apr 4 '18 at 15:39
  • Sounds like you might have a next pointer without a defined value. Have you removed the check for hashtable[index] being NULL, so it's always new_node -> next = hashtable[index]; hashtable[index] = new_node;?
    – Blauelf
    Apr 4 '18 at 16:07
  • I did indeed! And, I found the wonderful DJB2 hash function. Passed in a lowercase value and hence have successfully executed the program. Thanks for your inputs. I have also updated the code on the gist. It runs on 0.08 seconds, which is a more than the staff solution, but I am happy that atleast it works for now. Apr 4 '18 at 16:30
  • I have one question though, why does increasing the number of buckets have no effect on the speed of the program? If you take a look at the updated gist, I tried various number of buckets from 7,000 to 15000 currently, but to no effect at the lookup time. Apr 4 '18 at 16:34

You must log in to answer this question.

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