2

There are a few problems here. First, there is a logic issue in adding nodes to the tree: else { n->next = head; table[index] = n; } This works once, but on the next pass, head is still pointing at one node while table[index] points at another. This could be fixed, but the better thing to do would be to ...


2

Did you know that sizeof(word) returns the length of the word variable? That's right, 8 bytes on a 64-bit system. The variable stores a memory address of the first byte of the string (stored somewhere else). You meant strlen instead. Not sure how this can be related to a memory leak, but it's clearly a potential out-of-bounds access. BTW, if you used it ...


2

Perhaps there's a slight oversight in your code. Look at the following: lc_word[len + 1] = '\0'; Now, say that the word is "Cat". Length is 3 so len = 3. Remember that arrays start at 0, not 1. So, "Cat" or "cat" would occupy 0, 1 and 2. The line of code above would put the end of string marker at lc_word[3+1], or lc_word[...


2

The problem with the misspelled words lies in check, specifically with this: while (cursor->next != NULL) // wonder if this should be while (cursor != NULL) ? Your wondering is exactly on target. ;-) The infinite loop lies in the unload function. perhaps you've made it a little overcomplicated? If this answers your question, please click on the check ...


2

Actually, the problem lies in load. free(n); This is corrupting the dictionary tree. Remember, n is a pointer to memory. When you free it, you free that memory. Even if it has been added to the linked lists of the tree, that particular memory is being freed. The bottom line is that immediately after the words are stored in the tree, their nodes are ...


1

speller.c is responsible for sending the words to your check function. So it's not your code. Notice that speller only recognizes a word if it ends in punctuation or whitespace. Your test text's last word doesn't, so it's not sent to check.


1

After several false starts, we finally got the code to test. Have you tested your code with a 45 char word in a dictionary? The code is generating a seg fault when the dictionary is composed of exactly 1 word of 45 chars. That seg fault is in unload(). I'm highly confident that you haven't looked for the problem there yet, so I'll give you a chance. You ...


1

The reason for the seg faults lies in check() It uses a do/while setup that checks whether temp is null after running the loop. If the bucket was empty and table[hashed] is NULL, then it's going to generate a seg fault. This is highly likely with a small dictionary. Use a standard while loop instead. The next problem is that check() tries to hash whatever ...


1

Although you're doing a case-insensitive comparison between the word to be checked and the dictionary words, the code is hashing the word with capital letters. That means that any word with a capital letter will get a different hash than the same word with all lower case letters. There may be other issues. If this answers your question, please click on ...


1

fscanf(file , "%s" , word) By design, fscanf automatically put '\0' in the end. So if it reads cat\n, the result is cat\0. By doing this: word[strlen(word) - 1] ='\0' you overwrite the last char of the word. So cat\0 transforms to ca\0\0. --Updated after comments--- Yes, there is another mistake in load(). When you add new word and try to insert new ...


1

Your code node *checker = malloc(sizeof(node)); /* ... */ checker = hashtable[index]; means you allocate a new node, and then you throw away the only reference to that node, leaving this node as "definitely lost". There is no need to malloc in check. You copy pointers (memory addresses), not nodes. Same goes for the free, remove it, you might ...


1

I took a very quick glance at it and see this: if (strcmp(word, wordcheck->word)) { return true; } This tells me that you've assumed that strcmp returns true/false. It does not. If the strings are identical, it returns 0, which evaluates as false. If the strings are different, it will return either a positive or a ...


1

No, not at all. You're conflating two different var names to be the same variable. Let's look at the code: // Returns true if word is in dictionary else false bool check(const char *word) { ... if strcasecmp(cursor-> word, tword) !=0) cursor= cursor-> next; The variable tword is indeed derived from the variable word. But, this word is the name ...


1

You're on the right track. The problem is definitely within the hash function. I added a printf statement to print every word being checked to find out which word was triggering the seg fault. The culprit is a'n't. So, looking back at your hash function, there's a big oversight. What happens when the second or third letter is an apostrophe? Right now, ...


1

Hmmm, interesting problem. ;-) I made a few assumptions on declarations, but they appear to be right. So, let's look at the core block of code that inserts the word into the trie. for (int i = 0, n = strlen(word); i < n; i++) { // Create index in node corresponding to the current letter or '\'' [0-26] index = get_index(word[i]); ...


1

sizeof(word) sounds like what you want (here for (int i = 0; i < sizeof(word); i++) {, but it is not. sizeof returns how many bytes the argument uses in memory. Iin this case word is a pointer, and sizeof(apointer) is 4 (32bit machine) or 8 (64bit). Nevertheless you want to know the length of word, so strlen


1

Ugh! I figured it out. I was returning "false" from the load function. Can't believe I missed that! Resolved.


1

check should not allocate any node (just set your traversal pointer to hashtable[hash_value]) Your problem is likely in the load function, where you allocate your nodes and place them in the hash table. It seems you are creating some loop in the linked list. Maybe you place the next pointer incorrectly, or you re-use a node instead of creating another.


Only top voted, non community-wiki answers of a minimum length are eligible