Reputation: 151
I would like how to free the memory that a pointer whose address was generated by a functions. I think the code below explains the situation.
while((fgets(line,MAXLINELEN,stdin))!=NULL)
{
char *temp = format_name(strtok(line, "\n")); // format name, as posted below, allocates memory via malloc and returns a pointer
search(*dict1, temp, 0, 0, argv[2]); // this functions uses the temp above
free(temp); // this seems natural for me, but the program doesn't work properly if I do that.
}
The function:
char *format_name (char *line)
{
char *temp = malloc(sizeof(char) * (strlen(line) + 1));
*(temp + 0) = *(line + (int)strlen(line) - 1);
int i = 0;
while (*(line + i) != '\n' && *(line + i) != '\t' && i < (strlen(line) - 2))
{
*(temp + i + 1) = *(line + i);
i++;
}
return temp;
}
I know that the pointer created by the function is different from the pointer that I declared in the while loop from the first code, but as they (hopefully) point to the same address, I thought it should work. If I use the free, however, the program compiles and runs, but it generate a wrong output (the output is not the string temp).
What am I doing wrong? Is there a more elegant usage for this kind of situation?
EDIT:
As the code above is right, here's the search function:
void search(tree *l, char *key, int i, int n, char *filename)
{
if (*(key + 0) == '\0')
{
return;
}
if (l == NULL)
{
if (n == 0)
{
FILE *fp;
fp=fopen(filename, "a");
fprintf(fp, "%.*s\t\t%.*s\tNOTFOUND\n", ( (int)(strlen(key)) ), (key + 1), 1, key);
fclose(fp);
}
printf("%.*s\t\t%.*s\t%d\n", ((int)strlen(key) ), (key + 1), 1, (key), n );
return;
}
printf("we are comparing %s and %s\n", key, l->key);
if (strcmp(key, l->key) < 0)
{
printf("left...\n");
search(l->left, key, i + 1, n, filename);
}
else if (strcmp(key, l->key) > 0)
{
printf("right...\n");
search(l->right, key, i + 1, n, filename);
}
else
{
FILE *fp;
fp=fopen(filename, "a");
fprintf(fp, "%.*s\t\t%.*s\t%d\n", ((int)strlen(key) ), (key + 1), 1, (key), l->number );
fclose(fp);
search(l->right, key, i + 1, n + 1, filename);
}
}
Expected file (actual result without free):
Andre P NOTFOUND
Crombie R 98024839
Kounovsky X 92737902
Glader R 97039865
Swim E 87039991
Fraunfelter Q 96558147
Netkowicz X 84804603
Sferra I 94137883
Vadasy J 83543659
Nguyan A 81755418
Lardner L 82266784
Generated: (it changes every time I run, but there's always a lot of "NOTFOUND")
Andre P NOT FOUND
Crombie R 98024839
Kounovsky X 92737902
Gladersky R NOTFOUND
Swimersky E NOTFOUND
Fraunfelter Q 96558147
Netkowiczer X NOTFOUND
Sferraiczer I NOTFOUND
Vadasyiczer J NOTFOUND
Nguyaniczer A NOTFOUND
Lardnerczer L NOTFOUND
Upvotes: 1
Views: 191
Reputation: 39551
You're not zero terminating the string you generate in format_name
. When you free the string each time through the loop you're likely allocating the same region of memory each time, meaning that the previous key value will still be stored there. When you don't free the memory, malloc has to allocate a new region of memory, and apparently you're getting memory that hadn't been allocated already and initialized to 0.
To fix your bug just add a temp[i + 1] = 0;
line before the return
statement in format_name
.
Upvotes: 2