0

Code for load is below. When I go to GDB and print new->word a couple steps after the while statement (with the fscanf), it gives the following: "\000\000\000\000\203\212\343\267\003\000\000\000\204\361\377\277\224\361\377\277\352\314\376\267\003\000\000\000\204\361\377\277$\361\377\277(\260\004\b\340\202\004\b\000\220"

And yet, later, when I check the value assigned to my hashtable, it will say "cat". What is it I'm looking at? And what can I do differently?

    bool load(const char* dictionary)
    {    
        // Open the dictionary
        FILE* source = fopen(dictionary, "r");
        if (source == NULL)
        {
            fclose(source);
            fprintf(stderr, "Could not load dictionary.\n");
            return false;
        }

        // Make a new word and hash it
        int index = 0;
        node* new = malloc(sizeof(node));    
        if(new == NULL)
        {
            return false;
        }

        while(fscanf(source, "%s\n", new->word) != EOF)
        {
            // Hash the word
            index = toupper(new->word[0]) - 'A';

            // Check if hash table slot blank
            if (HASHTABLE[index] == NULL)
            {
                HASHTABLE[index] = new;
            }
            // Prepend
            else
            {                              
                 // Add to head of list
                 new->next = HASHTABLE[index];
                 HASHTABLE[index] = new;
            }

            // create new node
            node* new = malloc(sizeof(node));

            if(new == NULL)
            {
                return false;
            }

            // initialize new node
            new->next = NULL;

            index = 0;
        }

        return true;
    }
3
  • Exactly what command are you using to display new->word? Is it possible that you are actually displaying the memory address of new->word and not the contents? – Cliff B Dec 27 '15 at 18:07
  • Oh yeah, should have put that. In GDB I'm writing: print *new (which shows me both the elements word and next. But I can also do print new->word and will get the same content. – Norcim133 Dec 28 '15 at 0:09
  • I've seen elsewhere people mention that this starts with a null character "\0" and so we can ignore the rest. Which is fine. But the first word in the dictionary is "cat" meaning we'd expect to see cat\0 at the start. Even weirder, my hash function yields the right value for cat... suggesting the right value is somehow in there??? – Norcim133 Dec 28 '15 at 0:33
0

You are declaring two nodes named new, one outside the while loop, the other inside. gdb displays the value of the outer new until the first time you declare the new new inside the while loop. Here is a small repro snippet that demonstrates:

Breakpoint 1, main (argc=2, argv=0x7fffffffdf88) at q15570.c:30
30 printf("the word is %s\n",new->word);
(gdb) next
the password is cat
31 index = toupper(new->word[0]) - 'A';
(gdb) print *new $1 = { word = "\000\000\000\000\000\000\000\000\305\016s\367\377\177\000\000\000\000\000\000\000\000\000\000\210\337\377\377\377\177\000\000\000\000\000\000\002\000\000\000p\a@\000", next = 0x0}
(gdb)

You must log in to answer this question.

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