memory leaks

This is a discussion on memory leaks within the C Programming forums, part of the General Programming Boards category; Hey guys im having some memory problems with my linked lists. Im freeing the memory after it has been allocated ...

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    memory leaks

    Hey guys im having some memory problems with my linked lists. Im freeing the memory after it has been allocated so why is it still complaining?



    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct node 
    {
    
      int x;
      struct node *next;
    
    }node;
    
    
    int main(void)
    {
       
       node *head = NULL;
       node *conductor = NULL;
       head = malloc(sizeof(struct node));  
       conductor = head;
      
    
      
     
    
       if(conductor !=0)
       {
            while(conductor->next !=0)
            {
               conductor = conductor->next;
            }
       }
    
       conductor->next = malloc(sizeof(struct node));
    
       conductor = conductor->next;
       
       if(conductor == 0)
       {
          printf("out of memory\n");
       }
       
        
       conductor->next = 0;
       conductor->x = 57;
    
       conductor = head;
    
       if(conductor !=0)
      {
       while(conductor->next !=0)
        {
         printf("%d", conductor->x); 
         conductor = conductor->next;
          
        }
      }
       conductor = conductor->next;
    
       free(head);
       free(conductor);
       return EXIT_SUCCESS;
    }

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You're only free'ing the head and the conductor, not every element of the list. Imagine that you append a certain number of elements to your list:

    A->B->C->D->...->Z

    head will point to A and conductor will point to another (random) element. If you free them, there will still be N-2 elements left to free if you have N elements in your list.
    You need to iterate over all the elements and free them one by one. Don't forget to save the conductor->next in a seperate pointer before freeing or else you lost the link to the next element:

    Code:
    node *tmp;
    while (conductor->next != NULL)
    {
    tmp = conductor->next;
    free(conductor);
    conductor = tmp;
    }
    and please use NULL for pointers, it's easier to read than 0.

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    I really hope the new operator nullptr comes into force when the standards are reviews in a few years. Would make Nulling a pointer to much clearer
    I'm just trying to be a better person - My Name Is Earl

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    memory leaks

    Ok i understand that now. But does that mean i would also have to free the head but i would only free that once because that is a node which is only itself. Im still gettin leaks as well


    Code:
    #include <stdio.h>
    
    typedef struct node 
    {
    
      int x;
      struct node *next;
    
    }node;
    
    
    int main(void)
    {
       
       node *head = NULL;
       node *conductor = NULL;
       node *tmp;
       head = malloc(sizeof(struct node));  
       conductor = head;
       
    
       if(conductor !=0)
       {
            while(conductor->next !=0)
            {
               conductor = conductor->next;
            }
       }
    
       conductor->next = malloc(sizeof(struct node));
    
       conductor = conductor->next;
       
       if(conductor == 0)
       {
          printf("out of memory\n");
       }
       
        
       conductor->next = 0;
       conductor->x = 57;
    
       conductor = head;
    
       if(conductor !=0)
      {
       while(conductor->next !=0)
        {
         printf("%d", conductor->x); 
         conductor = conductor->next;
          
        }
      }
       conductor = conductor->next;
       
       while(conductor->next != NULL)
       {
       tmp = conductor->next;
       free(conductor);
       conductor = tmp;
       }
       free(head);
       return EXIT_SUCCESS;
    }

  5. #5
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    How can you start the free'ing process right after you printed the whole list, in which case the conductor is at the end of the list! You have to set the conductor to point to the head before starting to free.

  6. #6
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    so would i go conductor = head;

  7. #7
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Quote Originally Posted by bazzano View Post
    so would i go conductor = head;
    what do you think ? did you test it ?

  8. #8
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    yehhh and it says in the leak summary i have 8 bytes which are deffinately leaking? but it now says that both memory allocations have been freed.

  9. #9
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    sorry guys im still hving some troublei have 1 definate leak 8 bytes in my program not sure where iv deallocated all the nodes

  10. #10
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Well, a pointer has 4 bytes already and you have several of them, maybe that's what he's reporting. It can't be a structure since that would be 8 bytes per structure (if int is 4 bytes), so I guess it's the pointers. Try setting them to NULL at the end.

  11. #11
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    ok kewl

  12. #12
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    i fixed it i wasnt freeing tmp variable i was freeing the head instead. Yey!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking for memory leaks
    By Bladactania in forum C Programming
    Replies: 5
    Last Post: 02-10-2009, 11:58 AM
  2. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  3. Tons of memory leaks
    By VirtualAce in forum C++ Programming
    Replies: 11
    Last Post: 12-05-2005, 09:19 AM
  4. COM Memory Leaks
    By subdene in forum Windows Programming
    Replies: 0
    Last Post: 06-07-2004, 11:57 AM
  5. about memory leaks with this simple program
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 04-07-2002, 07:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21