strdup()
is causing memory leaks everytime it's used in readLine()
(bc of its auto malloc) bc Im not freeing/deallocating the memory right.
I've tried to get the adress of the word and many other methods for hours now but with no progress.
I'm doing a calloc bc I'll be storing an undetermined #of words.
This has been hardcoded (only 3 words) as to show the working example. If I get a huge amount of words in the file, the memory leak will be too important.
Text:
JOHN VELMA ZORRO
#include "stdio.h"
#include <stdlib.h>
#include <string.h>
struct link {
char *word;
struct link *next;
};
void readLine(FILE *file, char **words) {
int i = 0;
char ligne[80];
while (fgets(ligne, 80, file)){
char *word = strtok(ligne, " ,.-\n");
while (word != NULL) {
words[(i) ] = strdup(word); //<<<<<<<<<<<<<<<<<<<<<<<<<<<
word = strtok(NULL, " ,.-\n");
}
}
fclose(file);
}
int main(int argc, char *argv[]) {
FILE *file = fopen(argv[1], "r");
char **words = calloc(3, 30 * sizeof(char *) 1);
readLine(file, words);
struct link *head = malloc(sizeof(struct link));
struct link *ptrr = head;
head->word = words[0];
head->next = NULL;
for (int i = 1; i < 4; i) {
struct link *ptr, *temp;
if (words[i] != 0) {
ptr = head;
temp = malloc(sizeof(struct link));
temp->word = words[i];
temp->next = NULL;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
int a = 0;
while (ptrr != NULL) {
printf("Noeud#%d ->%s\n", a, ptrr->word);
ptrr = ptrr->next;
a ;
}
//////freeing **words
struct link *tmpp;
while (head != NULL) {
tmpp = head;
head = head->next;
free(tmpp);
}
for (int i = 0; i < 4; i) {
free(words[i]);
}
free(words);
return 0;
}
CodePudding user response:
no , strdup is not causing a memory leak, you are by not freeing the memory it allocates. I note that you dont free any of your memory (the calloc and mallocs you do explictly)
strdup is this
char *strdup(const char*s){
int len = strlen(s) 1;
char * res = malloc(len);
if(res != NULL)
strcpy(res, s);
return res
}
there is no magic, its just a nice utility function that encapsulates a common set of operations.
You have to free the memory it returns, just like all the other allocations
CodePudding user response:
THe reason you leak with a bigger file is this
You read the entire file into words (in readline). Note that you only reserve enough spaces for 30 words, so thats a potential error
while (fgets(ligne, 80, file)) {
char* word = strtok(ligne, " ,.-\n");
while (word != NULL) {
words[(i) ] = _strdup(word);
word = strtok(NULL, " ,.-\n");
}
}
so lets say there are 15 words you strdup 15 times
Then you build a list
for (int i = 1; i < 4; i) { <<<=======
struct link* ptr, * temp;
if (words[i] != 0) {
ptr = head;
temp = malloc(sizeof(struct link));
temp->word = words[i];
temp->next = NULL;
while (ptr->next != NULL)
ptr = ptr->next;
ptr->next = temp;
}
}
but you only take the first 4 words.
Then when you free you only free those 4
struct link* tmpp;
while (head != NULL) {
tmpp = head;
head = head->next;
free(tmpp);
}
for (int i = 0; i < 4; i) { <<<<=============
free(words[i]);
}
free(words);
the other 11 are leaked