Having a problem with freeing memory in the end of the program. This is an exercise from school, implementation of a binary-tree with ADT and specific implementation with data-type char.
freeing code:
void free_tree(TreeNode *root){
TreeNode *cur;
if (!root) return;
else{
cur = root;
free_tree(cur->left);
free_tree(cur->right);
free(cur->key); //throws an error!
free(cur);
} }
This is where i malloc for the key itself (problem is probably here):
puts("Please enter a value for key of new node");
_flushall();
scanf("%s",&buffer);
user_input = (char *) malloc(sizeof(char)*(strlen(buffer)+1));
strcpy(user_input,buffer);
user_input[strlen(buffer)+1] = '\0';
p_node = create_tree_node(user_input); //this function append the new data to a new node, returns *TreeNode
insert_node_by_value(&root,p_node,str_comp);
break;
And this is the error I get:
BTW Freeing the node themselves is working fine!
I'll appreciate your help and any tips and comments about the functionality of the code.
Full code paste of functions.c can be found here: http://pastebin.com/TqaNK5v8 - functions