1

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:

Error Message

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

Clifford
  • 88,407
  • 13
  • 85
  • 165
Lulu
  • 438
  • 5
  • 15

4 Answers4

1

The only actual problem I could find is:

 user_input[strlen(buffer)+1] = '\0';

Since user_input has 'strlen(buffer) + 1' elements, it's only indexable from 0 to strlen(buffer). To solve this is very simple, remove the line, strcpy behavior is to copy the \0 of the source string.

Caian
  • 440
  • 4
  • 15
1

99 chanches over 100 is that cur->key is not allocated or it is free'd more than once.

Also

user_input[strlen(buffer)+1] = '\0';

should be

user_input[strlen(buffer)] = '\0';

and buffer must have been allocated before scanf to hold the input data retrieved with scanf.

Can't tell more looking at the code you pasted

Hope this helps

Clifford
  • 88,407
  • 13
  • 85
  • 165
Paolo
  • 15,233
  • 27
  • 70
  • 91
  • Another probability is that write-access to an adjacent allocation has overrun and corrupted the heap management data prefixed to each allocation. – Clifford Jan 19 '13 at 10:51
0

Array indexing starts from 0. So, if you are allocating n bytes, the right way to access nth byte is::

SomeArray[n-1] ;
Abhineet
  • 5,320
  • 1
  • 25
  • 43
0

You're projecting your misguided diagnosis onto others. Please provide a minimal compilable testcase for us to debug. Minimal means "displaying the symptoms of your problem without any unnecessary or irrelevant code". Compilable means "able to compile on our machines without fixing errors or filling in the blanks".

In C, you don't need to cast the return value of malloc. Stop using a C++ compiler to compile C code. Are you aware that scanf can fail? I suspect this is why you are erroneously fflushing before scanf. Try handling the scanf failures correctly, rather than assuming non-portable hacks will do it for you. Are you aware that strcpy will null-terminate the destination string for you?

Which books are you reading?

autistic
  • 1
  • 3
  • 35
  • 80
  • Had no idea about the compiler. I'm aware of scanf, it's not the purpose of the homework so i don't mind about it too much anyway. I'm now aware of strcpy copies the null-terminate. Not reading books - I'm reading my teacher's lectures – Lulu Jan 19 '13 at 12:46