Hot answers tagged

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 ...


5

When I hit my road block here, I went back to the source code and re-watched the walkthrough videos, specifically this one: https://www.youtube.com/watch?v=OnAItxJhS70 There are subtle hints throughout the video that made it all click for me. (It helped that I printed out the code to write notes on as I watched.) In particular, study how request() works. ...


4

You want function load() to work for both: files that have been opened by fopen() pipes that have been opened by popen() I also did not manage to make it work by using fseek I solved the problem by using fgetc(), reading all bytes one by one and at the same time counting them so that I could determine *length too in the end. The more bytes you read the ...


4

The parameter being passed in is not a file pointer, it's the name of the file, a text string. That name is used inside of load() to open the file pointer locally. A pointer could be passed, but since parameters are passed by copy, it arguably makes a bit more sense to pass the file name in as a text string and let the pointer be opened locally inside ...


4

You would need to use debug50 or gdb to "stop the program and inspect", not check50. Since you are asking for help with debugging, my comments are intentionally cryptic. If load returns false, you will get a 500 error. Without using gdb et.al. review your function and find the place where it returns true. debug50/gdb should make it clear that load will ...


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 ...


3

While I may or may not agree with the previous comment, none of those things will cause any issues. Your problem is here: else { node *tmp_node = malloc(sizeof(node)); tmp_node -> next = hashtable[i] -> next; hashtable[i] -> next = new_node; } So, you nave new_node-> word, which already has the word store. You ...


3

The seg fault happens because the code is trying to make an assignment to something that doesn't exist. Look at the following code block: if(!root[index]) { temp=malloc(sizeof(node)); strcpy(temp->name,nm); root[index]->next=temp; The if condition checks to see if root[index] ...


3

Here are specific things to look at: load Declare c as an int instead of size_t, since size_t is unsigned. indexes Your allocation for path2 is wrong char* path2 = (char*)malloc(sizeof(strlen(php) + strlen(html) + 1)); First, the sizeof has got to go. It will probably return 4, since the argument will be an integer. Next, path2 is going to be either ...


3

For this line: letterIndex = c - '\''; try to see what happens if c == 'a', the ASCII code for '\'' is 39. Try fixing this and see what happens


3

This is an ineffective use of a hash function. In essence, your load function simply inserts each word sequentially into an array, along with a hash number, and the check function is doing a linear search. It might as well be implemented without the hashing, at least it would be a little faster. Bottom line is this is incredibly inefficient. The purpose of ...


3

check: tolower(word[i]) returns the lowercased ith character in word. You never use the value, you might want to assign it. Or remove this loop and later use tolower(word[i]) instead of word[i] when calculating a. Instead of if(tmp->is_word==true) return true; else return false; just do return tmp->is_word; load: Be ...


3

This while (fscanf(input, "%s", buffer) != EOF) executes an fscanf, followed immediately by this fscanf (input, "%s", buffer);, therefore it is processing every other word.


2

The hash table that you've loaded the words into should be globally accessible since many functions will use it and a direct access would make things way easier. Also, you're not supposed to alter the declaration of load and since it returns bool, you have to return a bool value. The value true should be returned in case the loading process ended with a ...


2

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


2

For steps 2-3, the data structure (e.g., the hash table in your case) that's gonna be used to store the words in the dictionary and the array of pointers to variables of that type both should be created/declared as global not local to load(). For step 5, as since you're reading char-by-char, you should stop as you hit a newline character as each word in the ...


2

Should I try to improve it more? Yes, if you wish! Does it matter? It's meant to challenge the staff and yourself at the first place, so officially it doesn't matter. If I should improve more how can I do it? Particularly in load as that's where I'm 0.02 seconds slower. My unload is 0.01 seconds faster. If you're using a hashtable, then the hash ...


2

Regardless of whether the code does what it should do or not, the problem is likely to be caused by the fact that you are trying to access cursor->next when cursor is actually a null-pointer. This would be the situation in case tabla[index] was the only node in this linked-list and newnode->word does not equal tabla[index]->word which would mean ...


2

This is the way strings work in C. Because a string in C is an array of chars, you can only initialize it during the declaration. If you want to change its contents after the initialization, or if you declared it without initialization, you need to manually copy the new string into the old string char by char, and to not forget about the final '\0'. Or you ...


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

I will give you the pseudocode and let you convert it to actual code yourself. if hashtable[hash] is NULL make hashtable[hash] point to new_node else create a temporary_node make temporary_node point to where hashtable[hash] is pointing while temporary_node not NULL make temporary_node point to where temporary_node->next is ...


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

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

There is one mistake here: while (fgetc(ptr) != EOF) doesn't just work as a condition, but it does read a char and moves on, so next time you read a string it's gonna miss the first char of the word because it was read by fgetc(). Fix that and report back. I would suggest you use the fscanf() in your loop condition instead (but then remove it from inside ...


2

Actually, your problem is a lot more severe than it appears. You are getting a massive number of false positives. I say that they're false because you're not actually checking the words correctly. So let's start with that. But first, some good news. Load is working correctly. At the bottom of your search function, you have a catch-all statement, return ...


2

TL; DR: This looks like a memory leak. Remove: node *tableindex = malloc(sizeof(node)); tableindex = hashtable[ind]; ... free(tableindex); Add: node *tableindex = hashtable[ind]; comparison of the original and updated code (I took some guesses at your hash function etc..) Why: It seems the main problem is how the node pointer tableindex is ...


2

TL; DR: Change if (fscanf(input, "%45s", new_node -> word) != EOF) to if (fscanf(input, "%45s", new_node -> word) == EOF) and new_node -> next = hashtable[n] -> next; hashtable[n] -> next = new_node; to new_node -> next = hashtable[n]; hashtable[n] = new_node; Too Many Words: Firstly, it seems the conditions are flipped on ...


2

The problem is that you're trying to assign content to vars that don't exist. Look at the following: if (hashtable[index] == NULL) { strcpy(hashtable[index] -> word , buffer); hashtable[index] -> next = NULL; hashtable[] is an array of pointers to nodes, not nodes. At this point (the very first word), the pointer is null. ...


2

A global variable is one that can be used anywhere in the program. Generally, globals are considered to be a bad practice, but there are times where they are perfectly appropriate for the situation. This is one of those cases. (There are those that say a global should NEVER be used. When you find one of those people, always evaluate their advice with an ...


2

In your load, when your hashtable[key]->next isn't null, you do this: new_word->next=head; head = new_word; what is head at this point? Last time you set it, it was for the first node in your linked list. As such, you are overwriting the first node in that bucket and end up throwing away some words. Shouldn't you be using hashtable[key]->...


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