c - Memory leak when freeing linked list -


I am learning, and I am using time for memory management in C. I have written this program after learning about related links:

  #include & lt; Stdio.h & gt; # Include & lt; Stdlib.h & gt; Structure node {int x; Structure node * next; }; Zero free_node (Structure node * n) {struct node * p, * Next; For (p = n, p! = Null; p = next) {next = p-> next; Free (P); }} Int main (int argc, char const * argv []) {int i; Structure node * n, * p, * root = malloc (sizeof (structure node)); Root- & gt; X = 0; N = root; For (i = 1; i & lt; = 5; ++ i) {struct node * Next = Molec (formation (structure node)); Next-> x = i; N- & gt; Next = next; N = n- & gt; next; } N-> Next = null; // Price for print (p = root; p! = Null; p = p-> next) {printf ("% i \ n", p-> x); } Free_Node (N); Return 0; }  

I have read that most of the tutorials have done a bad job while explaining memory management in C, if it is to be mentioned at all. I would point that this code will free the memory of all Molok, but when I think it will bring: valgrind --track-origins = yes --leak-check = full ./linked_list Valgrind tells me it the (I left the first part of the message that was not really information):

  0 1 2 3 4 5 == 1366 == == 1366 == Hiap summary: == 1366 == Exits on exit: 29,618 bytes in 381 blocks == 1366 == Total Hep Use: 460 total, 79 free, allocated 35,650 bytes == 1366 == == 1366 == 80 (16 direct, 64 indirect) 1 block In bytes Surely the loss is lost in the record 46 = 78 == 1366 == at 0x6DFB: Mallok (/usr/local/Cellar/valgrind/3.9.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so) == 1366 == 0x100000E87 By: Main (in / linked_list) == 1366 == == 1366 == Leak summary: == 1366 == Definitely lost: 16 bytes in 1 block == 1366 == Indirectly lost : 64 blocks in 4 blocks = = 1366 == Probably lost: 0 blocks in block 0 = 1366 == Still accessible: 1 block 4,096 bytes == 1366 == Pressed: 25,442 bytes IN 375 blocks == 1366 == Retrievable Lokon (for that was a pointer) are not shown. == 1366 == to see them, they run again: --leak-check = full --show-leak-kinds = all == 1366 == == detected for 1366 == and down the number of errors, re-run with them: - V == 1366 == error summary: 2 references from 2 errors (buried from 148 49)  

I saw, and memory The leak can not be found. I would love to know why this is happening, and in the future, what can I do to stop memory leaks? Thanks!

You are free to n , but it does not point to links Start the list ... Try calling free_node :

  free_node (root);  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -