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 ...
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. ...
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 ...
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 ...
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 ...
should I let the array point to NULL when I define the struct in the
If your struct definition looks like this
struct node *arr;
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 ...
While I may or may not agree with the previous comment, none of those things will cause any issues. Your problem is here:
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 ...
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:
The if condition checks to see if root[index] ...
Here are specific things to look at:
load Declare c as an int instead of size_t, since size_t is unsigned.
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
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 ...
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.
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 ...
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 ...
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
If you're using a hashtable, then the hash ...
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 ...
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 ...
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. ...
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
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 ...
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 ...
Why not just create an array of pointers to nodes?
struct node* foo;
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. ;-)
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 ...
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 ...
This looks like a memory leak.
node *tableindex = malloc(sizeof(node));
tableindex = hashtable[ind];
node *tableindex = hashtable[ind];
comparison of the original and updated code (I took some guesses at your hash function etc..)
It seems the main problem is how the node pointer tableindex is ...
if (fscanf(input, "%45s", new_node -> word) != EOF)
if (fscanf(input, "%45s", new_node -> word) == EOF)
new_node -> next = hashtable[n] -> next;
hashtable[n] -> next = new_node;
new_node -> next = hashtable[n];
hashtable[n] = new_node;
Too Many Words:
Firstly, it seems the conditions are flipped on ...
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. ...
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 ...
In your load, when your hashtable[key]->next isn't null, you do this:
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]->...