7

You can do it. It's a good practice to initialize the values to something, NULL in this case, to prevent any garbage value remaining in the memory from messing with the execution of the code. I don't know for sure but I think you can't do this in the declaration of the struct, you can implement a function to do that, or since probably you are going to ...


7

Hmmm..... 568 bytes in 1 block.... sounds like a file pointer. Did we forget to close an open file somewhere???? ;-) Side note: valgrind usually tells you how you can get more information, with something like For counts of detected and suppressed errors, rerun with: -v If this answers your question, please click on the check mark to accept. ...


3

should I let the array point to NULL when I define the struct in the beginning? If your struct definition looks like this struct node { struct node *arr[27]; }; then you actually can't assign a value to arr. And I don't actually recommend having arr declared as a pointer then allocating memory for it on the heap since if you do that you'll need to ...


2

Try removing the semicolon after your if statement. if(d == NULL) { printf("Could not load %s\n",dictionary); return false; }


2

You overlooked something. Here's a hint. What is the integer value of ' - 'a'? Or, subtract the ASCII value of 'a' from the value of an apostrophe? I'm actually surprised that your code isn't generating an error. Oh, wait, it does, sort of. Try turning off the printing of misspelled words and run against a large text under valgrind --leak-checks=full. ...


2

For the structure given by node above, memory can be allocated using the following syntax: LETTERNODE * lettterptr = malloc(sizeof(LETTERNODE)); or more universally: struct node * letterptr = malloc(sizeof(struct node));


2

Jessup: There's a few things here: 1) If you're calling a recursion on a function with a return value, like in this case, you should return each recursive call. This has nothing to do with the program hanging, but it will cause for it not to return the value expected. 2) You can't compare 2 strings with == or >/<. Look with gdb the result you get from ...


2

Why not just create an array of pointers to nodes? struct node* foo[27]; Then you can use a loop to assign NULL to each element by indexing through the array. If this answers your question, please click the check to accept, and remove your question from the unanswered question pool. Let's keep up on forum maintenance. ;-)


2

Your error originates a lot earlier in the code. Look at the first two lines in your do loop: a = fgetc(file); int hash_value = hash(word); Your first problem is that you only read a single character in from the file. You never actually read in a whole word anywhere. Next, you are calling hash() based on having a word in the string var word, but you've ...


2

one block of memory can be pointed at by many pointers. this is exactly what happens when you allocate memory for one pointer, then set the value of another pointer to be the same as the first pointer's value. example n = malloc(sizeof(node)); head = n; this allocates a block of memory on the heap, sets n to point to that block, then sets head to point to ...


2

The trie starts at one root node. This node will always have the is_word bool as false because the root node doesn't actually represent a letter. For a one letter word, say a, you would first move one level into the tree, as in root->children[0] before setting that node's is_word to true. (a two letter word, like ad, would traverse the path of root->...


2

One problem is here: word[strlen(word) - 1] = '\0'; You would need to "null out" the newline if using fgets. Not so with fscanf, it does not include the newline. From man fscanf: s Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and ...


2

Do not replace the value of root within check. That way, you replace your trie with an empty one. Here in Germany we sometimes call that a "Guttenbug", a bug by faulty copy&paste. (Semi-funny background: Former minister "Guttenberg" had his PhD thesis done with lots of Ctrl+C/Ctrl+V. In my opinion, he paid somebody and did not bother to read it. ...


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

The issue lies in how you are initializing your nodes. for (int i=0; i<27; i++) { trav->next[x]=NULL; } What is x on that line? Pay special attention to your counting variable in the loop. To answer your question about calloc, calloc will give you initialized (to zero) memory, which means you don't have to do the initialization loop. I would ...


1

A couple things I see right away... You malloc space for root here in check. However, you shouldn't need to do that. You probably already did this in load. The logic of check is such that you traverse the trie you created in load to see if the word given as input to this function was a word loaded into your dictionary previously. You are able to do that by ...


1

You are trying to free the same node twice. At the end of your freenode() function, you have the following code: if(n != NULL) free(n);. Remember that this is a recursive function. freenode() will recursively call itself until it gets to the bottom of the trie. Once there it frees all of the children at that level, and then the parent of the children that ...


1

I haven't tested the code, but I have several observations. First, do you actually have the code fully working? If not, you really should focus on that first. Correcting memory leaks before getting it working often just complicates the debugging. Here's a red flag: // create the base array of the alphabet node* alphabet[25]; There are 26 letters in the ...


1

Try using calloc instead of malloc (the syntax is slightly different, but you can google it, it's not difficult). The difference is that calloc initializes all the memory (in this case you might want to zero initialize it). malloc doesn't. So, on line 112, when you do if (current->children[index] == NULL) Here there's no guarantee that it will (or ...


1

Yes, you're not giving the right parameters. There are two ways to invoke speller: ./speller <file_to_test> ./speller <dictionary_file> <file_to_test> You've been invoking with one or both parameters that don't lead to files. If you invoke with only one parameter, it will use the default dictionary, large. If you invoke with both ...


1

At the bottom of your code, there is a place where your function will not return anything. That's probably around line 93 in your program, the line that clang complained about. Now you need to follow the logic, either on paper or with gdb, to see what conditions(s) will get you to that spot. One pair of braces in your code stands out because of their ...


1

Yes, the dictionary file is expected to be a file that contains a sorted list of words, one per line. Your mission is, indeed, to create in the LOAD function, code that will take those words and insert them into a tree or trie in memory so that you can check words from a test file against them later. LOAD will either take a command line parameter that ...


1

Simple problem. Your word_count is declared locally in the load() function. As soon as you leave load, it disappears. It does not exist outside of load. The word count is displayed in the call to size(). I'm guessing that you declared word_count again there. That is a different variable and is probably still 0. This is a case where a global variable is ...


1

I think the problem might be here: char* word[LENGTH + 1] = {}; If you use the star and the brackets, you are declaring an array of pointers to chars. Try instead just char word[LENGTH +1] = {}; When your code tries to copy your array of pointers into the node's word, it realizes they aren't the same type? I might be wrong, but give it a shot. HTH If ...


1

Figured it out! The problem was on dictionary.c:179. I replaced return freeMem(pntr->next[i]) with freeMem(pntr->next[i]) and now valgrind is happy!


1

THis should get you started. I ran your code using a tiny dictionary (cat, cater, car) as both the dictionary and the test text and was able to duplicate the seg fault. I located the following issue. Look at this code near the end of your check() function. else { while (linkedListCrawler->next != NULL); { linkedListCrawler = ...


1

It looks to me like you have a much larger problem than you think. It's reporting 143091 errors, and it looks like they all have the same origin. Look at the central part of your unload code: node* temp = next; free(next); next = temp->next; You load the address of next into temp, then free the memory at that address....


1

Your problem is indeed in your load function. Think about what happens when you read the last letter of the first word in your dictionary. You get outside of the for loop iterating over its letters, you count one more word, you set the boolean is_word to true and then you proceed to the next word. But when you started to load this word, your initial node, ...


1

I see a couple of problems here. First, in newword->next=malloc(LENGTH + 1);, 'newword->next' is a pointer to a node. You are allocating the space for newword->word, but not for the rest of the node, so sooner or later, you're not going to have enough space allocated to it. BUT, that's a moot point, because you'll never load anything into it. Instead, ...


1

Try changing this else if (strcmp(&c, "'") == 0) into this else if (c == '\'') EDIT Try running make speller instead of make dictionary


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