c - Valgrind newbie, can't seem to make it happy -
i'm working through cs50 course online, , task @ hand load dictionary memory (i'm using chained hash table data structure), , check contents of text file against words in dictionary. wrote load() function loads dictionary file , stores every word memory. function returns true when words loaded. valgrind shows no memory leaks. problem have ridiculous amount of read/write errors. helped me pinpoint seem coming , i've been scouring interwebs can't seem make heads or tails of it.
here's relevant code:
#include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #define hash_size 10 #define length 45 typedef struct node { char* word; struct node* next; } node; // globals node* hashtable[hash_size]; unsigned int dictionary_size = 0; /** * loads dictionary memory. returns true if successful else false. **/ bool load(const char* dictionary) { // initialize variables char currentword[length + 1]; int tableindex; // open dictionary file* dict = fopen(dictionary, "r"); if (dict == null) return false; while(fscanf(dict, "%s", currentword) == 1) { // hash value of word tableindex = hash(currentword); // initialize new node node* newnode = malloc(sizeof(node)); newnode->word = malloc((strlen(currentword) + 1) * sizeof(char)); if (newnode == null || newnode->word == null) { printf("error: out of memory\n"); return false; } // copy word new node strcpy(newnode->word, currentword); newnode->next = null; // if no collision, hash word head of list if (hashtable[tableindex] == null) hashtable[tableindex] = newnode; // create pointer , move down list else { node* ptrnode = hashtable[tableindex]; while (ptrnode->next != null) ptrnode = ptrnode->next; // append node end of linked list ptrnode->next = newnode; } // increase dictionary size dictionary_size++; // free word member before actual node free(newnode->word); free(newnode); } // close dictionary , return true fclose(dict); return true; }
and valgrind:
==32487== invalid read of size 4 ==32487== @ 0x8048989: load (my_new_test.c:120) ==32487== 0x804873d: main (my_new_test.c:53) ==32487== address 0x423b30c 4 bytes inside block of size 8 free'd ==32487== @ 0x402b3d8: free (in /usr/lib/valgrind/vgpreload_memcheck-x86- linux.so) ==32487== 0x80489d3: load (my_new_test.c:132) ==32487== 0x804873d: main (my_new_test.c:53) ==32487== ==32487== invalid write of size 4 ==32487== @ 0x80489aa: load (my_new_test.c:124) ==32487== 0x804873d: main (my_new_test.c:53) ==32487== address 0x423b30c 4 bytes inside block of size 8 free'd ==32487== @ 0x402b3d8: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==32487== 0x80489d3: load (my_new_test.c:132) ==32487== 0x804873d: main (my_new_test.c:53) ==32487== ==32487== invalid read of size 4 ==32487== @ 0x8048999: load (my_new_test.c:121) ==32487== 0x804873d: main (my_new_test.c:53) ==32487== address 0x423bb24 4 bytes inside block of size 8 free'd ==32487== @ 0x402b3d8: free (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so) ==32487== 0x80489d3: load (my_new_test.c:132) ==32487== 0x804873d: main (my_new_test.c:53) ==32487== heap summary: ==32487== in use @ exit: 0 bytes in 0 blocks ==32487== total heap usage: 286,183 allocs, 286,183 frees, 2,584,308 bytes allocated ==32487== ==32487== heap blocks freed -- no leaks possible ==32487== ==32487== counts of detected , suppressed errors, rerun with: -v ==32487== error summary: 10000000 errors 3 contexts (suppressed: 0 0)
i'm still raw valgrind gather issue seems inside else statement, pointer created (node* ptrnode). can see i'm not seeing? appreciated!
the problem these 2 lines:
free(newnode->word); free(newnode);
you added node hash-table, free it, making pointers in hash-table invalid point free'd data.
Comments
Post a Comment