Thread: need help in free() of linked list

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    8

    need help in free() of linked list

    hey, i'm have a "heap corruption" error while trying to free linked list.
    my structs are:

    Code:
    typedef struct client
    {
    	char* License;
    	struct client* next;
    }client;
    
    typedef struct station
    {
    	char* sName;
    	client* clients;
    	struct station* next;
    }station;
    linked list of station, each with linked list of clients. stations and clients have a name type char* allocated by malloc.

    i'm trying to free the list like this:

    Code:
    void FreeAll(station *st)
    {
    	client *cl;
    	station* temp;
    	while (st != NULL)
    	{
    		cl = st->clients;
    		FreeClients(cl);
    		temp = st;
    		st = st->next;
    		free(temp->sName);
    		free(temp);
    	}
    }
    
    void FreeClients(client* cl)
    {
    	client* temp;
    
    	while (cl != NULL)
    	{
    		temp = cl;
    		cl = cl->next;
    		free(temp->License);
    		free(temp);
    	}
    }
    when i get to free(temp->License); in FreeClients the program crashs down, using debugging i get "heap corruption" error...

    thenks for your time...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Since it's a char pointer, my guess is you forgot to count the \0 when allocating space for a string.

    To copy "hello", you need 6 chars, not 5.
    That is
    p = malloc( strlen(s) + 1 );
    strcpy( p, s );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Not sure how would that effect freeing a pointer? I suppose free, free the allocated memoryt back to the pool. My guess would be that he's freeing NULL pointer.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Freeing NULL pointer is absolutely fine. FYI...

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Perhaps I should have said, pointer which hasn't initialised.
    Code:
    char *p;
    free(p);   <- crash
    Though it is very unlikely.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    well, i can say for sure that i'm note trying to free a NULL pointer, although even if i did that sould be fine...

    i did find the root of the problem, it IS as u guys sayed, a problem with free(char*), i did allocated anough space:

    Code:
    client *cl = (client*)malloc(sizeof(client));
    cl->License = (char*)malloc(sizeof(str)+1);
    strcpy(cl->License, str);
    even so, when it gets to freeing that freaking char*, the program crushs down...

    thenk you all for helping out... the deadline is in 24 hours, and the freeing the allocated memory is the only one standing in my way :\

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > cl->License = (char*)malloc(sizeof(str)+1);
    Do you know the difference between strlen() and sizeof() ?

    There's almost no difference if str is an array.
    But if str is a POINTER, then your sizeof is just plain broken.

    It does NOT tell you how long the string is!

    Oh, and read the FAQ on not casting the result of malloc in a C program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2009
    Posts
    8
    thenk you salem...

    i did sizeof when allocating a struct, forgot to change to strlen when allocating string, that was the problem.

    thenx again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. free the memory of linked list.
    By xinwu in forum C Programming
    Replies: 2
    Last Post: 11-02-2010, 01:48 PM
  2. Linked List help!!
    By onepuzzledstud in forum C Programming
    Replies: 2
    Last Post: 10-11-2010, 08:36 PM
  3. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM