I'm writing a simple program in C that finds anagrams of words based on looking them up in a hash table. What I have is an array of hash tables, where words are indexed by their length and hashed within by the summation of their letters, e.g. a = 1, b = 2, etc.
I'm still getting acquainted with dynamic memory management in C, so this question may be quite simple, but I have a functions that allocate and deallocate memory for each hash table (and its inside data).
I originally wrote this program using a single hash table and after running the program through valgrind I found that the memory had been freed correctly and there were no leaks. However, when expanding the program to use an array of hash tables, valgrind began to find possible leaks (though it's reporting them as still reachable). I'm confused as to why the memory isn't being freed correctly in the array of hash tables, although each hash table in the array is being run through the deallocate function that was used originally.
Gist with the full code Full Code
typedef struct Bucket Bucket;
typedef struct HashTable HashTable;
struct Bucket {
char* data;
Bucket *next;
};
struct HashTable {
int size;
Bucket **buckets;
};
int main(int argc, char const *argv[])
{
// Allocate memory for array of hash tables
HashTable** hash_array = (HashTable**) malloc(sizeof(HashTable*) * WORD_SIZE);
for(i = 0; i < WORD_SIZE; i++) {
hash_alloc(&hash_array[i], BUCKET_COUNT);
}
// Main logic here...
// Free memory
for(i = 0; i < WORD_SIZE; i++) {
hash_dealloc(hash_array[i]);
}
free(hash_array);
return 0;
}
Hash Table Allocation function
void hash_alloc(HashTable** a, unsigned int size) {
*a = (HashTable*) malloc(sizeof(HashTable));
(*a)->buckets = (Bucket**) malloc(sizeof(Bucket*) * size);
(*a)->size = size;
}
Hash Table Deallocation function
void hash_dealloc(HashTable* a) {
int i;
Bucket* current, *temp;
for(i = 0; i < a->size; i++) {
current = a->buckets[i];
while(current != NULL) {
temp = current;
free(temp->data);
current = current->next;
free(temp);
}
free(current);
}
free(a->buckets);
free(a);
}
Add to hash table function
void add_to_hash_array(HashTable** a, char* data) {
// Removed some other logic for readability...
replace_str(data, "\n", "\0");
newNode->data = strdup(data);
newNode->next = currentTable->buckets[index];
currentTable->buckets[index] = newNode;
} else {
return;
}
}
Valgrind output
==39817== 261,120 bytes in 128 blocks are still reachable in loss record 5 of 7
==39817== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==39817== by 0x400A38: hash_alloc (main.c:73)
==39817== by 0x4008B0: main (main.c:39)
==39817==
==39817== 286,936 bytes in 31,553 blocks are still reachable in loss record 6 of 7
==39817== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==39817== by 0x4EBAD71: strdup (strdup.c:43)
==39817== by 0x400D4D: add_to_hash_array (main.c:141)
==39817== by 0x400914: main (main.c:51)
==39817==
==39817== 504,848 bytes in 31,553 blocks are still reachable in loss record 7 of 7
==39817== at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==39817== by 0x400D16: add_to_hash_array (main.c:136)
==39817== by 0x400914: main (main.c:51)
In generate_anagrams
void generate_anagrams(HashTable* a, char* word) {
int index = hash(word);
char* word_dup = strdup(word);
char* current_word;
string_sort(word_dup);
printf("%s: %i size: %zi\n", word, index, strlen(word));
Bucket* current = a->buckets[index];
while(current != NULL) {
if(current->data) {
current_word = strdup(current->data);
string_sort(current_word);
if(strcmp(word_dup, current_word) == 0) {
printf("%s\n", current->data);
}
}
current = current->next;
}
free(current_word);
free(word_dup);
}
you are leaking thestrduped current_words. Move the free(current_word); into the if (current->data).
In add_to_hash_array
void add_to_hash_array(HashTable** a, char* data) {
int index = hash(data);
HashTable* currentTable = a[strlen(data)];
Bucket* existingNode = hash_lookup(currentTable, data);
if(existingNode == NULL) {
Bucket *newNode = (Bucket*) malloc(sizeof(Bucket));
if(newNode == NULL) {
exit(1);
}
replace_str(data, "\n", "\0");
newNode->data = strdup(data);
newNode->next = currentTable->buckets[index];
currentTable->buckets[index] = newNode;
} else {
return;
}
}
You only remove the newline from data after you have looked it up, but you insert data after removing the newline, so you will not detect duplicates.
In main, you should not
generate_anagrams(hash_array[strlen(arg_dup) + 1], arg_dup);
but use hash_array[strlen(arg_dup)] if you remove the newline right at the beginning of add_to_hash_array.
And of course, you should ensure that hash generates no out-of-range indices.
And strncpy(str, str, p-str); is undefined behaviour.
Moving my comment to an answer because this nearly works.
No need to initialise to NULL before malloc, but any variable pointers that you later free should be set to NULL and only free if not NULL. Also initialise pointer values in your structs to NULL (easier to use calloc and get this automatically) so your guards work properly. I've had a bit more of look over your code and your hashing worries me - you need to make sure your hash function only returns values in the range 0 to BUCKET_COUNT-1. I made a few quick changes of this kind, and fixed your replace_str method and it no longer complains with valgrind.
Now when I run the code I see it finding the correct words to compare with, but the sorting function is not returning a sorted string, so it is not identifying the anagrams. Should be an easy fix.
Edit: I just pasted in the first string sort routine a google search found for me and ran to get the following output:
./a.out trips
trips: 82 size: 5
strip
stirp
sprit
spirt
Ther's no memory leak, but the code has some problems (the replace_str function need to be replaced by a correct version). The output of valgrind on my linux box:
$ valgrind --leak-check=full --show-reachable=yes ./MEMORY_LEAK absurde
==13476== Memcheck, a memory error detector
==13476== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==13476== Using Valgrind-3.6.1-Debian and LibVEX; rerun with -h for copyright info
==13476== Command: ./MEMORY_LEAK absurde
==13476==
==13476== Conditional jump or move depends on uninitialised value(s)
==13476== at 0x8048A9A: hash_lookup (MEMORY_LEAK.c:132)
==13476== by 0x80489BD: add_to_hash_array (MEMORY_LEAK.c:113)
==13476== by 0x804871C: main (MEMORY_LEAK.c:50)
==13476==
absurde: 70 size: 7
==13476== Conditional jump or move depends on uninitialised value(s)
==13476== at 0x8048D1C: generate_anagrams (MEMORY_LEAK.c:169)
==13476== by 0x8048795: main (MEMORY_LEAK.c:56)
==13476==
==13476== Conditional jump or move depends on uninitialised value(s)
==13476== at 0x8048890: hash_dealloc (MEMORY_LEAK.c:81)
==13476== by 0x80487D8: main (MEMORY_LEAK.c:64)
==13476==
==13476== Conditional jump or move depends on uninitialised value(s)
==13476== at 0x4027BC2: free (vg_replace_malloc.c:366)
==13476== by 0x804889C: hash_dealloc (MEMORY_LEAK.c:87)
==13476== by 0x80487D8: main (MEMORY_LEAK.c:64)
==13476==
==13476==
==13476== HEAP SUMMARY:
==13476== in use at exit: 0 bytes in 0 blocks
==13476== total heap usage: 360 allocs, 360 frees, 133,424 bytes allocated
==13476==
==13476== All heap blocks were freed -- no leaks are possible
==13476==
==13476== For counts of detected and suppressed errors, rerun with: -v
==13476== Use --track-origins=yes to see where uninitialised values come from
==13476== ERROR SUMMARY: 65330 errors from 4 contexts (suppressed: 11 from 6)
Related
I am making a project using a hash array. I am putting an image i found online to show you guys what i'm trying to do. image
So I've initialized the big array on the left like this in main:
cellule_t * array[HASH_MAX] = {NULL};
Then i have made all the insertions to create the linked lists. What i want to do now, is to free() all the memory allocated to create all this. So, like I'm not using malloc for the big array, i just need to free the linked lists and I'm using this function :
But i get some errors from Valgrind and the blocks are not freed.
I'm putting all the functions I use including the main to make it more clear.
typedef struct cell{
char *mot;
char *traduction;
struct cell *suivant;
}cellule_t;
int main()
{
int indice;
cellule_t *cel;
FILE*file = NULL;
char buffer[100] = "hello bye glass sorry";
cellule_t *tabMajeur[HASH_MAX] = {0};
file = fopen("fichier.txt","r");
remplissage_hachage(tabMajeur,file); (c:157)
affichageTraduction(tabMajeur,buffer);
libererMemorie(tabMajeur); (c:159)
}
void remplissage_hachage (cellule_t **tabMajeur,FILE *fichier)
{
char string1[20];
unsigned int indice = 0;
cellule_t *copy;
int boolean = 0;
char *string2, *string3 = NULL;
cellule_t *c =NULL;
while(fgets(string1,100,fichier) != NULL)
{
string2 = strtok(string1,";");
string3 = strtok(NULL,";");
string3[strlen(string3)-1] = '\0';
printf("string2 %s\n",string2);
printf("string3 %s\n",string3);
indice = hash_string(string2);
boolean = 0;
indice = recherche(tabMajeur,string2,&boolean,&c);
if(boolean != 1)
{
copy = tabMajeur[indice];
tabMajeur[indice] = creationCellule(string2,string3); (c: 64)
tabMajeur[indice]->suivant = copy;
}
}
}
cellule_t* creationCellule(char * mot, char *traduction)
{
cellule_t *nouveau = (cellule_t *) malloc(sizeof(cellule_t)); (c:24)
if(nouveau != NULL)
{
nouveau -> mot = strdup(mot);
nouveau -> traduction = strdup(traduction);
nouveau -> suivant = NULL;
}
return nouveau;
}
void libererMemorie(cellule_t **tabMajeur)
{
int i = 0;
cellule_t * cour;
cellule_t * copy;
for(i = 0 ; i < HASH_MAX; i++)
{
cour = tabMajeur[i];
while(cour != NULL)
{
copy = cour;
free(copy); (c:137)
cour = cour->suivant; (c:138)
}
}
}
The valgrind error is:
enter image description here
The text for valgrind error is:
==2550== Memcheck, a memory error detector
==2550== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==2550== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==2550== Command: ./tp4
==2550==
string2 hello
string3 bonjour
string2 bye
string3 au revoir
string2 sorry
string3 pardon
string2 glass
string3 verre
La traduction est bonjour au revoir verre pardon
==2550== Invalid read of size 8
==2550== at 0x108E55: libererMemorie (tp4.c:138)
==2550== by 0x108F85: main (tp4.c:159)
==2550== Address 0x522ea40 is 16 bytes inside a block of size 24 free'd
==2550== at 0x4C30D3B: free (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2550== by 0x108E50: libererMemorie (tp4.c:137)
==2550== by 0x108F85: main (tp4.c:159)
==2550== Block was alloc'd at
==2550== at 0x4C2FB0F: malloc (in
/usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==2550== by 0x108A5D: creationCellule (tp4.c:24)
==2550== by 0x108BD8: remplissage_hachage (tp4.c:64)
==2550== by 0x108F60: main (tp4.c:157)
==2550==
==2550==
==2550== HEAP SUMMARY:
==2550== in use at exit: 605 bytes in 9 blocks
==2550== total heap usage: 15 allocs, 6 frees, 5,821 bytes allocated
==2550==
==2550== LEAK SUMMARY:
==2550== definitely lost: 53 bytes in 8 blocks
==2550== indirectly lost: 0 bytes in 0 blocks
==2550== possibly lost: 0 bytes in 0 blocks
==2550== still reachable: 552 bytes in 1 blocks
==2550== suppressed: 0 bytes in 0 blocks
==2550== Rerun with --leak-check=full to see details of leaked memory
==2550==
==2550== For counts of detected and suppressed errors, rerun with: -v
==2550== ERROR SUMMARY: 4 errors from 1 contexts (suppressed: 0 from 0)
Can you please help me find out what's wrong?
The "Invalid Read" error may stem from the fact that while you have a singly-linked list for each bucket in your hash table, you are attempting to free the list node and then access the pointer to the next node after you've free'd the current node.
So basically here:
copy = cour;
free(copy);
cour = cour->suivant;
You are creating undefined behavior because you are freeing the node, then attempting to access the free'd memory via the cour pointer to get at the next pointer in the linked list. Assigning copy the value of cour only copied the pointer value itself. You then free'd the memory pointed to by both copy and cour, and then attempted to access the memory in a free'd block. Valgrind definitely won't like that.
You can simply change your ordering to the following:
copy = cour;
cour = cour->suivant;
free(copy);
Now you free the memory in the current node after copying out the pointer to the next node while the memory block is still a valid allocated memory block.
I'm trying to make a list of students that stores structs using a dynamically allocated array. For this I have the following structs:
typedef struct s_student
{
char name[64];
int matrikelNummer;
} student;
typedef struct s_studentList
{
student* students;
int numberOfStudents;
} studentList;
This is how I use them so far:
int main(int argc, char** argv) {
studentList* list = NULL;
list = (studentList*) malloc(sizeof(studentList));
list->numberOfStudents =1;
//list->students->matrikelNummer = 1;
//strcpy(list->students->name , "karim");
printf("numberOfStudents : %d\n" , list->numberOfStudents );
//printf("matrikelNummer : %d\n" , list->students->matrikelNummer);
//printf("name : %d\n" , list->students->name);
free(list);
return 0;
}
That seems to work with no problems. But when I try to assign data to the students (matrikelNummer or name) as outlined in the commented lines, I receive a Segmentation fault.
What am I doing wrong?
Not allocated pointers
The problem is that you do:
// list points to null after that line
studentList* list = NULL;
// allocate memory for the list struct
list = (studentList*) malloc(sizeof(studentList));
// set field inside list struct
list->numberOfStudents =1;
// list->students is a pointer, but pointers should point to something valid
// So the question is: Was list->students set to NULL?
// Or was it mallocated?
list->students->matrikelNummer = 1;
So you access list->students that is a pointer.
List itself was allocated via malloc so its fine.
But malloc reserves space for only the list object you want.
It does not mallocate anything else.
So list->students is a pointer that is not mallocated - that's why we get the segmentation fault error.
Solution to this problem is pretty straightforward - we have to allocate not only a list but all pointers we use (in this case its students member):
// That was earlier there:
studentList* list = NULL;
list = (studentList*) malloc(sizeof(studentList));
// After allocating place for list also allocate place for list->students:
list->students = (student*) malloc(sizeof(student));
Detection of invalid memory usage
In a case when you get segmentation faults or memory leaks it's good to know that there are plenty of tools to help programmers detect such a nasty errors.
One of them is Valgrind
Valgrind is available for Linux (and probably for Windows but buggy and untested).
It's awesome tools that can traverse your program and notify you about any leaks, invalid frees and usage of forbidden memory addresses.
Example usage of Valgrind:
# Compile your code
gcc list.c -o list
# Use Valgrind
valgrind --tool=memcheck ./list
And what valgrind shows:
==26761== Memcheck, a memory error detector
==26761== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==26761== Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info
==26761== Command: ./list
==26761==
==26761== Use of uninitialised value of size 8
==26761== at 0x4006A1: main (in /home/students/inf/p/ps386038/stack/list)
==26761==
==26761== Invalid write of size 4
==26761== at 0x4006A1: main (in /home/students/inf/p/ps386038/stack/list)
==26761== Address 0x40 is not stack'd, malloc'd or (recently) free'd
==26761==
==26761==
==26761== Process terminating with default action of signal 11 (SIGSEGV)
==26761== Access not within mapped region at address 0x40
==26761== at 0x4006A1: main (in /home/students/inf/p/ps386038/stack/list)
==26761== If you believe this happened as a result of a stack
==26761== overflow in your program's main thread (unlikely but
==26761== possible), you can try to increase the size of the
==26761== main thread stack using the --main-stacksize= flag.
==26761== The main thread stack size used in this run was 8388608.
==26761==
==26761== HEAP SUMMARY:
==26761== in use at exit: 16 bytes in 1 blocks
==26761== total heap usage: 1 allocs, 0 frees, 16 bytes allocated
==26761==
==26761== LEAK SUMMARY:
==26761== definitely lost: 0 bytes in 0 blocks
==26761== indirectly lost: 0 bytes in 0 blocks
==26761== possibly lost: 0 bytes in 0 blocks
==26761== still reachable: 16 bytes in 1 blocks
==26761== suppressed: 0 bytes in 0 blocks
==26761== Rerun with --leak-check=full to see details of leaked memory
==26761==
==26761== For counts of detected and suppressed errors, rerun with: -v
==26761== Use --track-origins=yes to see where uninitialised values come from
==26761== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
So it shows that you access invalid address in function main:
==26761== Invalid write of size 4
==26761== at 0x4006A1: main (in /home/students/inf/p/ps386038/stack/list)
==26761== Address 0x40 is not stack'd, malloc'd or (recently) free'd
And says the address (pointer is not even allocated)!
Correct C list implementation
If you want to implement a pointers structure that holds list of students
then one common approach is to put a pointer to the next student (next on the list) into the s_student structure.
And pointers to the first and last student into the student list.
One working example is the following I wrote myself:
#include <stdio.h>
#include <stdlib.h>
typedef struct s_student student;
struct s_student
{
char name[64];
int matrikelNummer;
// This will point to the next student on the list
student* nextStudent;
};
typedef struct s_studentList
{
student* student;
// This will point to the last available student
student* lastStudent;
// This will point to the first available student
student* firstStudent;
int numberOfStudents;
} studentList;
// Allocates the list
void allocStudentsList(studentList** list) {
if(list == NULL) return;
*list = (studentList*) malloc(sizeof(studentList));
(*list)->lastStudent = NULL;
(*list)->firstStudent = NULL;
}
// To add the student to the list
void addStudentToList(studentList* list, student studentData) {
if(list == NULL) return;
// Allocate a place for the next student
student* st = (student*) malloc(sizeof(student));
// If it's first student in the list
if(list->lastStudent == NULL) {
list->lastStudent = st;
list->firstStudent = st;
} else {
// The next student after the current last student will be the newly created one
list->lastStudent->nextStudent = st;
}
// Fill the student data
*st = studentData;
st->nextStudent = NULL;
// Set the last available student to the one created
list->lastStudent = st;
}
// To recurisvely free the students
void freeStudent(student* stud) {
if(stud->nextStudent != NULL) {
// Free next student recursively
freeStudent(stud->nextStudent);
}
free(stud);
}
// To free the students list
void freeStudentsList(studentList* list) {
if(list != NULL) {
freeStudent(list->firstStudent);
free(list);
}
}
// Function that prints single student and returns next one (after him on the list)
student* printStudent(student* stud) {
if(stud == NULL) return NULL;
printf(" * Student { matrikelNummer = %d }\n", stud->matrikelNummer);
// Return next student
return stud->nextStudent;
}
// Function that prints students list
void printStudentsList(studentList* list) {
if(list == NULL) return;
printf("StudentsList [\n");
student* current_student = list->firstStudent;
while(current_student != NULL) {
current_student = printStudent(current_student);
}
printf("]\n");
}
int main(int argc, char** argv) {
studentList* list = NULL;
allocStudentsList(&list);
// Create some student data
student st1;
st1.matrikelNummer = 1;
// Another student...
student st2;
st2.matrikelNummer = 2;
// Put them into the list (allocates students and take care of everything)
addStudentToList(list, st1);
addStudentToList(list, st2);
// Print the list
printStudentsList(list);
// Free the list (recursively free's all students and take care of all the nasty stuff)
freeStudentsList(list);
return 0;
}
There are plenty of tutorials how to write C-style lists structures on the web.
You can find one yourself.
One of the tutorial is there: Learn-c linked list tutorial
in your s_studentlist struct you have a pointer called students, you never used malloc on this member so its just a pointer to a random chunk in memory, allocate it the same way you did with your list variable
you need to allocate memory for the student. When using malloc you only allocate memory for the list type, which includes a pointer to a student-type but not memory for the student type:
studentList* list = NULL;
list = (studentList*) malloc(sizeof(studentList));
list->students = (student*) malloc(sizeof(student));
Another Problem with you list is that I don't know how it is designed to work.
A list normally contains a pointer to the next element:
typedef struct s_student {
struct s_student* next;
char name[64];
int matrikelNummer;
} student;
Another approach would be to store a list of students as an array in studentList, but hten you would need to allocate memory for more than one student:
list->students = (student*) malloc(list->numberOfStudents * sizeof(student));
This would be no list, but an array and you would access entries with
list->students[0].matrikelNummer
So everything is fine with the program but i get a very annoying memory leak. I am sitting in front of my computer for couple hours and can figure it out.
We have 2 struck very simple, one struct is a double linked list and one is a hash table that stores that double linked list.
Now i am inserting a key and a data into the double linked list here is the function.
void htable_insert(htable* ht, int key, int data) {
// TODO: Insert a new entry with the given key and data
// Overwrite the old data if the key already exists, duplicate keys are not allowed
ht_entry *new_node;
ht_entry *head;
ht_entry *it;
int sameKey;
int bucketPosition;
new_node = (ht_entry*)malloc(1*sizeof(ht_entry));
bucketPosition = key % ht->size;
sameKey = 0;
for(it = ht->entries[bucketPosition]; it != NULL; it = it->next)
{
if(it->key == key) {
it->data = data;
sameKey = 1;
free(new_node);
new_node = NULL;
break;
}
}
if(!sameKey && new_node) {
head = ht->entries[bucketPosition];
if (head == NULL) {
new_node->key = key;
new_node->data = data;
new_node->next = head;
new_node->prev = NULL;
ht->entries[bucketPosition] = new_node;
new_node = NULL;
} else {
new_node->key = key;
new_node->data = data;
new_node->next = head;
// new_node->prev = head;
head->prev = new_node;
head = new_node;
ht->entries[bucketPosition] = head;
}
}
// free(new_node);
new_node = NULL;
printf("%s\n %d", "INSERT:", key);
for(it = ht->entries[bucketPosition]; it != NULL; it = it->next){
printf("it->key: %d\nit->data: %d\n", it->key, it->data);
}
printf("%s\n", "-------------------------------");
}
Here is my valgrind message:
==10692== Memcheck, a memory error detector
==10692== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==10692== Using Valgrind-3.10.1 and LibVEX; rerun with -h for copyright info
==10692== Command: ./chain_hash_table.out
==10692==
==10692==
==10692== HEAP SUMMARY:
==10692== in use at exit: 72 bytes in 3 blocks
==10692== total heap usage: 10 allocs, 7 frees, 376 bytes allocated
==10692==
==10692== 24 bytes in 1 blocks are definitely lost in loss record 2 of 3
==10692== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10692== by 0x4007EE: htable_insert (htable.c:53)
==10692== by 0x400BD2: main (main.c:14)
==10692==
==10692== 48 (24 direct, 24 indirect) bytes in 1 blocks are definitely lost in loss record 3 of 3
==10692== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==10692== by 0x4007EE: htable_insert (htable.c:53)
==10692== by 0x400C25: main (main.c:18)
==10692==
==10692== LEAK SUMMARY:
==10692== definitely lost: 48 bytes in 2 blocks
==10692== indirectly lost: 24 bytes in 1 blocks
==10692== possibly lost: 0 bytes in 0 blocks
==10692== still reachable: 0 bytes in 0 blocks
==10692== suppressed: 0 bytes in 0 blocks
==10692==
==10692== For counts of detected and suppressed errors, rerun with: -v
==10692== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
And what i know it is always for first table insertion that is why it says at line main(18) for the rest after the first insertion there is no leaks.
Thank you guys for your time and help :)
Check the break statement in the loop, it breaks on the first iteration
and it does not deallocate the node.
The break should be placed inside the for loop.
If you are mallocing space in a C program, unless you free everything at the end, you will end up with memory leaks.
For your current program, I'm assuming that you don't free correctly in the end. So the problem isn't within the htable_insert function, but rather within your freeing/cleanup functions.
If you free your linked list nodes in your htable_insert, you will not be able to access them in the rest of your program and you'll run into segfaults.
I have written a small implementation of a simple stack in C using a linked list. The application works after a bit of fussing with gdb and valgrind. However, I have noticed that valgrind reports a number of "still reachable" leaks at termination of the program.
After some googling, I have found that these type of leaks are not an issue and that I generally shouldn't worry about them. Well that's great news! The only thing is, I would REALLY like to understand why valgrind sees these leaks and where they appear. I went to great pains through my first pass at this application to diligently release any allocated memory. What did I miss? Where are these allocations that valgrind sees but I cannot?
I have been staring at this for awhile now, and despite fiddling with the code this way and that, have come up empty handed. Hopefully you all can help me in this endeavor.
I have stripped the app down to a minimal example that will present the leak I am observing. It just initializes the stack and then immediately destroys it and exits.
// simple stack implementation
#include <stdio.h>
#include <assert.h>
#include <string.h>
#include <stdlib.h>
/*--- data structure in a stack item ---*/
struct data {
char * message;
};
/*--- stack item structure ---*/
struct item {
struct data * my_data;
struct item * next;
};
/*--- stack data structure ---*/
struct stack {
int initialized;
int empty;
struct item * head;
};
struct stack * my_stack;
/*--- create a new stack data structure with provided initial data ---*/
int
create_stack(struct data ** initial_items, int num_items) {
//allocate memory for the stack structure itself
my_stack = (struct stack *) malloc(sizeof(struct stack));
if(!my_stack) return -1;
my_stack->empty = 1;
my_stack ->initialized = 0;
if(num_items) {
//allocate memory for the head of the list first
my_stack->head = (struct item *) malloc(sizeof(struct item));
if(!my_stack->head) return -1;
my_stack->head->my_data = initial_items[0];
my_stack->head->next = NULL;
struct item * tracker = my_stack->head;
//fill the stack with elements containing the provided data
int i = 1;
for(; i < (num_items); i++) {
tracker->next = (struct item *) malloc(sizeof(struct item));
tracker = tracker->next;
if(!tracker) return -1;
tracker->my_data = initial_items[i];
tracker->next = NULL;
}
//no longer empty
my_stack->empty = 0;
}
//set initialized flag & return
my_stack->initialized = 1;
return 0;
}
/*--- destroy the stack by recursively freeing nodes ---*/
int
destroy_stack(struct item * recurse) {
//if the starting node is valid, begin
if(recurse) {
//if the current node links to another
if(recurse->next)
//recurse on the next node
destroy_stack(recurse->next);
else
//otherwise we hit the end, free the node
free(recurse);
}
//the starting node is invalid, just free the stack itself
else {
free(my_stack);
}
return 0;
}
/*--- test wrapper ---*/
int
main(int argc, char **argv) {
//initialize 10 element list of data structures to fill the stack items with
int i = 0;
int length = 10;
struct data ** initial_values = (struct data **) malloc(length*sizeof(struct data *));
//set each data element's value to i
for(; i < length; i++) {
char temp[16];
sprintf(temp, "%d", i);
initial_values[i] = (struct data *) malloc(sizeof(struct data));
initial_values[i]->message = strdup(temp);
}
//simple test case
//create a stack with the generated initial data, then destroy it
assert( !create_stack(initial_values, length) );
assert( !destroy_stack(my_stack->head) );
//free the string data our stack nodes were pointing to
i = 0;
while(i < length) {
free(initial_values[i]->message);
free(initial_values[i]);
i += 1;
}
free(initial_values);
return 0;
}
Running this through valgrind produces the following identified, unreleased blocks:
(trusty)kin3tix#localhost:~/C Practice/Data Structures$ valgrind --leak-check=full --show-leak-kinds=all ./stack_leak_test
==19340== Memcheck, a memory error detector
==19340== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==19340== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==19340== Command: ./stack_leak_test
==19340==
==19340==
==19340== HEAP SUMMARY:
==19340== in use at exit: 168 bytes in 10 blocks
==19340== total heap usage: 32 allocs, 22 frees, 364 bytes allocated
==19340==
==19340== 16 bytes in 1 blocks are still reachable in loss record 1 of 3
==19340== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19340== by 0x400739: create_stack (stack_leak_test.c:40)
==19340== by 0x40093B: main (stack_leak_test.c:95)
==19340==
==19340== 24 bytes in 1 blocks are still reachable in loss record 2 of 3
==19340== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19340== by 0x4006E6: create_stack (stack_leak_test.c:33)
==19340== by 0x40093B: main (stack_leak_test.c:95)
==19340==
==19340== 128 bytes in 8 blocks are still reachable in loss record 3 of 3
==19340== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==19340== by 0x4007A1: create_stack (stack_leak_test.c:48)
==19340== by 0x40093B: main (stack_leak_test.c:95)
==19340==
==19340== LEAK SUMMARY:
==19340== definitely lost: 0 bytes in 0 blocks
==19340== indirectly lost: 0 bytes in 0 blocks
==19340== possibly lost: 0 bytes in 0 blocks
==19340== still reachable: 168 bytes in 10 blocks
==19340== suppressed: 0 bytes in 0 blocks
==19340==
==19340== For counts of detected and suppressed errors, rerun with: -v
==19340== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
They all seem to stem from "create_stack" but as far as I can tell my destroy function should be catching everything I allocate. What am I missing here?
Your destruction logic is trying to recursively combine two tasks (deleting the stack object and its items) in a single function. Worse, the else state shouldn't be there at all. You already determined you're sitting on a dynamic node (recurse). It needs to be deleted. Whether it has a next or not is not for the current depth to decide; that will be taken care of in the next depth of recursion.
Honestly, this is much simpler if you create a helper to wipe out the linked list, and as separate wrapper to do said-same to the stack itself:
/*--- destroy linked list nodes recursively ---*/
static void destroy_stack_items(struct item *item)
{
if (item)
{
destroy_stack_items(item->next);
free(item);
}
}
/*--- destroy the stack by recursively freeing nodes ---*/
int destroy_stack(struct stack *s)
{
if (s)
{
destroy_stack_items(s->head);
free(s);
}
return 0
}
and simply invoked as:
destroy_stack(my_stack);
Personally I would do it iteratively (thus remove the helper in the process), but I'm sure you have your reasons for otherwise. For example:
/*--- destroy the stack by iteratively freeing nodes ---*/
int destroy_stack(struct stack *s)
{
if (s)
{
while (s->head)
{
struct item *tmp = s->head;
s->head = tmp->next;
free(tmp);
}
free(s);
}
return 0
}
Best of luck.
Need help again.
I've got my linked list working. It copies a list of words from a file into a linked list.
Now I want to make a hash table so that all words starting with the letter 'A' go into a bucket [0] of linked list.
The code I've written; it seems to work for small and large lists but Valgrind shows points to errors.
-----CODE-----
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node //struct for linked list
{
char* word;
struct node* next;
}
node;
int findhash(char firstletter) //this function returns a hash value for first alphabet of every word
{
int hash = 0;
if(isupper(firstletter))
hash = firstletter-'A';
else hash = firstletter-'a';
return hash;
}
int main (void)
{
char* dictfile = "small";
FILE* dict = fopen(dictfile, "r");
if (dict == NULL)
return 1;
char oneword[47]; //to store the word from fscanf()
node* hashtable [30]; //creates a hashtable
for (int i = 0; i < 30; i++) //gives an index to every element in the hash table
{
node* temp = (node*)malloc(sizeof(node));
temp->next = NULL;
hashtable[i] = temp;
}
while ((fscanf (dict, "%s", oneword)) > 0)
{
node* temp = (node*)malloc(sizeof(node));
char* tempword = (char*)malloc(strlen(oneword)+1); //gives me a new pointer to store the string (as pointed out by Antione)
strcpy(tempword, oneword);
char firstletter = tempword[0];
int hash = 0;
hash = findhash(firstletter); //returns an index for the first alphabet of the word
temp->word = tempword;
//printf("%s\n", temp->word); //prints the value (just for debug)
temp->next = hashtable[hash];
hashtable[hash] = temp;
}
for (int i = 0; i < 30; i++)
{
node* temp = (node*)malloc(sizeof(node));
temp = hashtable[i];
while (temp != NULL) //loop to print the linked list
{
if (temp->word != NULL) //**THIS IS WHERE VALGRIND IS POINTING THE ERROR TO BE**
{
printf("%s\n", temp->word);
temp = temp->next;
}
else break;
}
}
printf("\n");
fclose(dict);
printf("SUCCESS!!\n");
}
Where am I going wrong?? Please help
-----VALGRIND ERROR-----
jharvard#appliance (~/Dropbox/pset5): valgrind ./tempeol
==11035== Memcheck, a memory error detector
==11035== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==11035== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==11035== Command: ./tempeol
==11035==
antione
==11035== Conditional jump or move depends on uninitialised value(s)
==11035== at 0x80488CA: main (tempeol.c:68)
==11035==
beer
bear
caterpillar
cat
google
root
SUCCESS!!
==11035==
==11035== HEAP SUMMARY:
==11035== in use at exit: 582 bytes in 74 blocks
==11035== total heap usage: 75 allocs, 1 frees, 934 bytes allocated
==11035==
==11035== LEAK SUMMARY:
==11035== definitely lost: 480 bytes in 60 blocks
==11035== indirectly lost: 102 bytes in 14 blocks
==11035== possibly lost: 0 bytes in 0 blocks
==11035== still reachable: 0 bytes in 0 blocks
==11035== suppressed: 0 bytes in 0 blocks
==11035== Rerun with --leak-check=full to see details of leaked memory
==11035==
==11035== For counts of detected and suppressed errors, rerun with: -v
==11035== Use --track-origins=yes to see where uninitialised values come from
==11035== ERROR SUMMARY: 30 errors from 1 contexts (suppressed: 0 from 0)
Valgrind shows 30 errors. My hashtable is size 30. So feel the problem should be somewhere there. But I can't figure out why?
Your initialization of the hashtable entries isn't initializing the field word, that is what valgrind is complaining about.
Additionally your code seems to have other problems. E.g the malloc in the line just before is immediately overwritten and thus this memory leaks. Also the return of malloc shouldn't be cast. Avoid casts whenever you can, think of casts as "casting a spell". As long as you don't know what that does, avoid it.