Freeing memory in recursive function: free(): double free detected in tcache2 (beginner question)
by lxs602 from LinuxQuestions.org on (#6MBT9)
Hi
I have a section of code that should loop through a hash table and delete a linked list if present, for a cs50 problem set.
Code:// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
} node;
// Number of buckets
const unsigned int N = 500041;
// Hash table
node *table[N];
// ... lines omitted
bool unload(void)
{
// Uncomment for loop method
// node *ptr, *ptr2;
for (int a = 0; a < (N + 1); a++)
{
// Loop method
/*
ptr = table[a];
while(ptr != NULL)
{
ptr2 = ptr;
ptr = ptr->next;
free(ptr2);
}
*/
// Recursion method
if (table[a] != NULL)
{
delete_node(table[a]);
}
}
// used in separate functions earlier in code:
free(counter);
return (!fclose(dictfile));
}
void delete_node(node *ptr)
{
if (ptr != NULL)
{
delete_node(ptr->next);
free(ptr);
}
return;
}I have a looped method (commented out) and a recursive method, but both give me the error in the subject line, which shows I am trying to free the same node twice.
I assume I have misunderstood something. What have I got wrong?
I have written what I think ought to be happening in pseudocode below:
- Recursive method: delete_node:
Quote:
- Looped method:
Quote:
Thank you
I have a section of code that should loop through a hash table and delete a linked list if present, for a cs50 problem set.
Code:// Represents a node in a hash table
typedef struct node
{
char word[LENGTH + 1];
struct node *next;
} node;
// Number of buckets
const unsigned int N = 500041;
// Hash table
node *table[N];
// ... lines omitted
bool unload(void)
{
// Uncomment for loop method
// node *ptr, *ptr2;
for (int a = 0; a < (N + 1); a++)
{
// Loop method
/*
ptr = table[a];
while(ptr != NULL)
{
ptr2 = ptr;
ptr = ptr->next;
free(ptr2);
}
*/
// Recursion method
if (table[a] != NULL)
{
delete_node(table[a]);
}
}
// used in separate functions earlier in code:
free(counter);
return (!fclose(dictfile));
}
void delete_node(node *ptr)
{
if (ptr != NULL)
{
delete_node(ptr->next);
free(ptr);
}
return;
}I have a looped method (commented out) and a recursive method, but both give me the error in the subject line, which shows I am trying to free the same node twice.
I assume I have misunderstood something. What have I got wrong?
I have written what I think ought to be happening in pseudocode below:
- Recursive method: delete_node:
Quote:
1. If pointer is not null, then run delete_node again with the pointer to the next node. 2. This will run recursively, passing along the linked list, until null is reached. when delete_node will return. 3. The second-to-last call to delete_node will then run, and free the final node in the linked list, and return, allowing the third-to-last call to delete_node to run, and so on down the stack. |
Quote:
1. Until ptr reaches null, at the end of the linked list, 2. Point ptr2 to ptr (e.g. to memory address "0x100") 3. Point ptr to the next node in the linked list (e.g. at memory address "0x101"), while ptr2 remains pointing to where ptr was (at "0x100") (is this right?) 4. Now free memory at ptr2 (at "0x100"), then while loop runs again for ptr (at "0x101"), if it is not null. |