0

I've been trying to fix this bug for the past couple of days, but it seems this one I really can't figure out on my own. Hope you guys can help a bit.

Here's the code for my load function in pset5.

#define HASH_SIZE 26
#define LENGTH 45

typedef struct node
{
char *word;
struct node *next;
}
node;

bool load(const char *dictionary){

The first part works: opening the file and creating an array of pointers for the hash table.

// Prepare hash table
node *head = NULL;
node *dict_table[HASH_SIZE];

FILE *dict_file = fopen(dictionary, "r");
if (!dict_file)
{
    return false;
}

// Fill hash table with pointers
int i = 0;
while (i < HASH_SIZE)
{
    dict_table[i] = head;
    i++;
}
// Prepare variables
int index = 0;
char *dWord = malloc(sizeof(char) * LENGTH);

I then iterate through the dictionary while checking for '\n' to find the words.

 // Prepare variables
int index = 0;
char dWord[LENGTH];

// Iterate over dictionary
for (char ch = fgetc(dict_file); ch != EOF; ch = fgetc(dict_file))
{
    // Word not found yet
    if (ch != '\n')
    {
        dWord[index] = ch;
        index++;
    }
    // Word found!
    else
    {
        dWord[index] = '\0';

        // Allocate new node
        node *new = malloc(sizeof(node));
        if(new == NULL)
        {
            return 1;
        }

        // Assign value and point head to new node
        new->word = dWord;
        new->next = head;
        head = new;

        // Update variables
        words++;
        index = 0;
    }
}
for (node *counter = head; counter != NULL; counter = counter->next)
{
    printf("LIST: %s\n", counter->word);
}
    return true;
}
}

The problem is that when I print the linked list at the end, the output equals n times the last word read from the dictionary. Somehow the list is not building correctly. I tried simplifying the code by getting rid of the for loop at the top and the conditions while only using a couple of words as input, which worked just fine.
I cannot figure out what goes wrong here! It must have something to do with the if conditions and the loop because the linked list stuff works on its own.
Thanks!

p.s. I'm not using the hash table array yet. Just trying to build a single linked list first.

2
  • Can you edit your question and add the struct declaration at the top please? Also, please add declarations for all globals, including dword.
    – Cliff B
    Feb 27, 2018 at 3:17
  • added the code! Feb 27, 2018 at 3:29

1 Answer 1

1
            new->word = dWord;

I'd say that the address of dWord is being copied into the pointer(s) new->word for every node created, and not the actual word. When every word is processed, it is written into dWord and every node points to it. So, when the last word is processed, every node "contains" the last word.

Instead of using a pointer for word in the structure, you might consider using a char array, word[LENGTH] instead and write the word directly into it. (Or, strcpy() might be useful too, if you don't write directly.)

That should get you going. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

[EDIT] Specifically, there are some changes needed to fix this. First, change the struct declaration:

typedef struct node
{
char  word[LENGTH+1];  // array to hold word + end of string marker
struct node *next;
}
node;

Next, either copy the letters of the word directly into new->word[] the same way letters are copied into dWord, or strcpy the word from dWord to new->word.

4
  • I tried changing dWord to a char array, but it doesn't solve the problem. The output is exactly the same! It could be something with dWord, because as I said when I tried a different input, using get_string, it worked fine. It's weird. Feb 27, 2018 at 3:56
  • of course it didn't work. You need to change the struct to contain a char array instead of a pointer, not dWord.
    – Cliff B
    Feb 27, 2018 at 4:07
  • I found a fix sort of! By using dWord = malloc(sizeof(char) * LENGTH) again after finding a word. This however causes memory leak. Do you think im going in the right direction? I tried changing the node struct but it didnt make any difference. I also got errors trying to assign a char array to new->word. Isn't char[] and char* the same thing sort of? Char[] is a pointer to the first element in the array, right? Feb 27, 2018 at 4:25
  • Yes, they both contain addresses, but no, not quite the same. char* is a pointer to a string. char myvar[45]; specifically defines a 45 char string and allocates the space for it on the stack. See my edited answer for more.
    – Cliff B
    Feb 27, 2018 at 4:42

You must log in to answer this question.

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