Free LINKED LIST in C - c

Here is the structures.
struct webPage{
struct wordOccurance *rootWord;
char URL[..];
};
struct wordOccurance{
struct wordOccurance *nextWord;
char word[...];
};
//Crawl and populate all words...
struct webPage *hopPage(url...){
[scan all words in the web page]
insertBackOfList(malloc(sizeof(struct wordOccurance)).. word)
}
//Now free..
void destroyPage(struct webPage *target){
while(target != NULL){
struct webPage *temp = target->next;
destroyWords(target->rootWord);
free(target);
target = temp;
}
}
void destroyWords(struct wordOccurance *target){
while(target != NULL){
struct wordOccurance *temp = target->nextWord;
free(temp);
target = temp;
}
}
==20169== Invalid read of size 4
==20169== at 0x8048DD5: destroyWords (index.c:109)
==20169== by 0x80492E2: destroyPage (crawler.c:166)
==20169== by 0x8049797: main (webSearch.c:91)
==20169== Address 0x41d5298 is 0 bytes inside a block of size 1,008 free'd
==20169== at 0x4024B3A: free (vg_replace_malloc.c:366)
==20169== by 0x8048DE4: destroyWords (index.c:110)
==20169== by 0x80492E2: destroyPage (crawler.c:166)
==20169== by 0x8049797: main (webSearch.c:91)
==20169==
==20169== Conditional jump or move depends on uninitialised value(s)
==20169== at 0x8049781: main (webSearch.c:90)
==20169==
==20169==
==20169== HEAP SUMMARY:
==20169== in use at exit: 52,432 bytes in 52 blocks
==20169== total heap usage: 95 allocs, 43 frees, 82,392 bytes allocated
==20169==
==20169== 1,008 bytes in 1 blocks are definitely lost in loss record 2 of 8
==20169== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==20169== by 0x8048A7F: indexPage (index.c:26)
==20169== by 0x8048F1D: hopURL (crawler.c:46)
==20169== by 0x8049570: main (webSearch.c:52)
==20169==
==20169== 1,008 bytes in 1 blocks are definitely lost in loss record 3 of 8
==20169== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==20169== by 0x8048A7F: indexPage (index.c:26)
==20169== by 0x80490E7: insertBack (crawler.c:108)
==20169== by 0x8048FAB: hopURL (crawler.c:66)
==20169== by 0x8049570: main (webSearch.c:52)
==20169==
==20169== 50,416 (1,012 direct, 49,404 indirect) bytes in 1 blocks are definitely lost in loss record 8 of 8
==20169== at 0x4024F20: malloc (vg_replace_malloc.c:236)
==20169== by 0x8048E8F: hopURL (crawler.c:28)
==20169== by 0x8049570: main (webSearch.c:52)
==20169==
==20169== LEAK SUMMARY:
==20169== definitely lost: 3,028 bytes in 3 blocks
==20169== indirectly lost: 49,404 bytes in 49 blocks
==20169== possibly lost: 0 bytes in 0 blocks
==20169== still reachable: 0 bytes in 0 blocks
==20169== suppressed: 0 bytes in 0 blocks
==20169==
==20169== For counts of detected and suppressed errors, rerun with: -v
==20169== Use --track-origins=yes to see where uninitialised values come from
==20169== ERROR SUMMARY: 356 errors from 13 contexts (suppressed: 14 from 7)

From both destroyPage() and destroyWords():
free(temp);
target = temp;
You are setting a variable to a pointer which you just freed. This is never correct.
The correct logical sequence to free a singly linked list is generally:
while (ptr) {
oldPtr = ptr;
ptr = ptr->next;
free(oldPtr);
}

Why are you setting target = temp after you just freed temp?
Remove that line and simply do:
struct wordOccurrence *temp;
while (target != NULL) {
temp = target;
target = target->nextWord;
free(temp);
temp = NULL; <-- optional
}
Your while loop was setting target to a node that was already freed, whereas in my excerpt, temp is reset to the next target node after every iteration, and then is subsequently freed.

Related

Valgrind errors for using free() in a linked list

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.

C memory leak that i can't find at all

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.

alloc and free of 2d array

I made 2d array(matrix) + alloc and free functions to manage memory but it isnt working well, valgrind prints many errors and information that memory was lost.
Alloc: parametr s means size of matrix
int** alloc(int s)
{
int** matrix;
int i;
matrix = (int**)malloc(s * sizeof(int*));
for (i = 0; i < s; i++)
{
matrix[i] = calloc(s, sizeof(int));
}
return matrix;
}
Free
void matrix_free(int*** matrix, int s)
{
int i;
for(i = 0; i < s; i++)
{
free(*((matrix)+i));
}
free(matrix);
}
Valgrind:
Many errors like this:
Invalid read of size 8
==3767== at 0x4FAA8D4: buffer_free (in /lib/x86_64-linux-gnu/libc-2.24.so)
==3767== by 0x4FAA942: __libc_freeres (in /lib/x86_64-linux-gnu/libc-2.24.so)
==3767== by 0x4A276EC: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
==3767== by 0x4E73292: __run_exit_handlers (exit.c:98)
==3767== by 0x4E73339: exit (exit.c:105)
==3767== by 0x4E593F7: (below main) (libc-start.c:325)
==3767== Address 0x52000e8 is 168 bytes inside a block of size 552 free'd
==3767== at 0x4C2DD6B: free (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3767== by 0x108BBB: matrix_free (m1.c:68)
==3767== by 0x108B69: main (m1.c:58)
==3767== Block was alloc'd at
==3767== at 0x4C2CB3F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3767== by 0x4EA7F2C: __fopen_internal (iofopen.c:69)
==3767== by 0x108919: main (m1.c:16)
==3767==
==3767==
==3767== HEAP SUMMARY:
==3767== in use at exit: 36 bytes in 3 blocks
==3767== total heap usage: 7 allocs, 6 frees, 5,732 bytes allocated
==3767==
==3767== LEAK SUMMARY:
==3767== definitely lost: 36 bytes in 3 blocks
==3767== indirectly lost: 0 bytes in 0 blocks
==3767== possibly lost: 0 bytes in 0 blocks
==3767== still reachable: 0 bytes in 0 blocks
==3767== suppressed: 0 bytes in 0 blocks
The free function in this case doesn't take a pointer that points to the allocated memory, but a pointer to that pointer.
To free the memory you need to first obtain that pointer.
void matrix_free(int*** matrix, int s)
{
int** m = *matrix;
int i;
for(i = 0; i < s; i++)
{
free( m[i] );
}
free( m );
*matrix = NULL;
}
This variant also enables you to set the argument to NULL:
matrix_free( &matrix , s );
assert( matrix == NULL );

Valgrind definitely lost block of memory

I am trying to figure out what is wrong with my valgrind debugging. I am starting to learn valgrind and there are some things I do not know how to solve
==12902== HEAP SUMMARY:
==12902== in use at exit: 40 bytes in 4 blocks
==12902== total heap usage: 21 allocs, 17 frees, 2,400,792 bytes allocated
==12902==
==12902== Searching for pointers to 4 not-freed blocks
==12902== Checked 77,768 bytes
==12902==
==12902== 40 (16 direct, 24 indirect) bytes in 2 blocks are definitely lost in loss record 2 of 2
==12902== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==12902== by 0x400EAB: elequeue_ini (elequeue_point.c:15)
==12902== by 0x400F90: elequeue_copy (elequeue_point.c:67)
==12902== by 0x4012EC: queue_insert (queue.c:105)
==12902== by 0x400C24: main (p3_e1.c:85)
==12902==
==12902== LEAK SUMMARY:
==12902== definitely lost: 16 bytes in 2 blocks
==12902== indirectly lost: 24 bytes in 2 blocks
==12902== possibly lost: 0 bytes in 0 blocks
==12902== still reachable: 0 bytes in 0 blocks
==12902== suppressed: 0 bytes in 0 blocks
==12902==
==12902== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
==12902== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Elequeue makes reference to the elements of a queue ADT. The codes involved are:
struct _EleQueue{
Point *info;
};
EleQueue* elequeue_copy(const EleQueue * src){
EleQueue *copied_ele;
if(src==NULL){
return NULL;
}
copied_ele=elequeue_ini();
if(copied_ele==NULL){
return NULL;
}
copied_ele->info=point_copy(src->info);
return copied_ele;
}
EleQueue* elequeue_ini(){
EleQueue *e;
e=(EleQueue*)malloc(sizeof(EleQueue));
if (e==NULL){
return NULL;
}
e->info=NULL;
return e;
}
Queue* queue_insert(Queue *q, const EleQueue* pElem){
EleQueue *auxElem;
if(!q || !pElem || queue_isFull(q) == TRUE){
return NULL;
}
auxElem=elequeue_copy(pElem);
if(auxElem == NULL){
return NULL;
}
*(q->end) = auxElem;
if(q->end == &(q->item[MAXQUEUE-1])){
q->end = &(q->item[0]);
} else {
q -> end++;
}
return q;
}

Why does my implementation of a dynamic string array leak?

I need a string array which dynamically resizes when more items are added to it. I got the basic code working, but valgrind reports memory leaks.
The implementation which should work with static and dynamically allocated strings looks like this:
typedef struct {
char **items;
int num;
} StringArray;
StringArray* string_array_init() {
StringArray *arr = malloc(sizeof(StringArray));
arr->items = NULL;
arr->num = 0;
return arr;
}
void string_array_add(StringArray *arr, char *str, int str_len) {
void *new_items = realloc(arr->items, (arr->num + 1) * sizeof(char *));
arr->items = (char**)new_items;
arr->items[arr->num] = strndup(str, str_len);
arr->num++;
}
void string_array_cleanup(StringArray *arr) {
int i;
for (i = 0; i < arr->num ; i++) {
free(arr->items[i]);
}
free(arr);
}
int main() {
StringArray *arr = string_array_init();
string_array_add(arr, "item 1", strlen("item 1"));
string_array_add(arr, "item 2", strlen("item 2"));
string_array_add(arr, "item 3", strlen("item 3"));
string_array_cleanup(arr);
return 0;
}
Valgrind reports:
==31443== HEAP SUMMARY:
==31443== in use at exit: 12 bytes in 1 blocks
==31443== total heap usage: 7 allocs, 6 frees, 53 bytes allocated
==31443==
==31443== 12 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31443== at 0x4025CCD: realloc (vg_replace_malloc.c:525)
==31443== by 0x80484A6: string_array_add (in ...)
==31443== by 0x8048593: main (in ...)
==31443==
==31443== LEAK SUMMARY:
==31443== definitely lost: 12 bytes in 1 blocks
==31443== indirectly lost: 0 bytes in 0 blocks
==31443== possibly lost: 0 bytes in 0 blocks
==31443== still reachable: 0 bytes in 0 blocks
==31443== suppressed: 0 bytes in 0 blocks
==31443==
Why is realloc leaking and how can I fix it? I thoght freeing every string separately and then freeing the struct would be enough, but I´m missing something.
You free the strings contained in arr->items, but you don't free arr->items itself. (You allocated it in string_array_add).
The problem is with this line:
arr->items = (char**)new_items;
You overwrite the old pointer. You need to realloc this pointer and not new_items.

Resources