Thread: Delete and destroy in double linked lists

  1. #16
    Registered User
    Join Date
    Jan 2010
    Posts
    34
    so here you free LISTdt which is on the stack or do you allocate ListDt and allocate after that the linked list ? if not then thats your problem and it would lead to other problem on noticed one in deleteDList in which ur deleting all including head.

  2. #17
    Registered User
    Join Date
    Mar 2010
    Posts
    31
    Quote Originally Posted by kira_coder View Post
    so here you free LISTdt which is on the stack or do you allocate ListDt and allocate after that the linked list ? if not then thats your problem and it would lead to other problem on noticed one in deleteDList in which ur deleting all including head.
    This is were I create my list
    Code:
    ListDT *createDList()
    // returns a pointer to the header cell of the created list
    {
    
     ListDT *List=(ListDT*) malloc( sizeof (ListDT) );
     if (List != NULL)
          {
           List->size=0;
           List->first = NULL;
           List->last = NULL;
          }
    
     return List;
    }
    My main.c has only this:

    Code:
    ListDT *L=NULL;
        L=createDList()
        destroyDList(L);
        printDList(L);
    I'm just trying to find out how this works.
    It runs at infinity, printing a large number over and over again (eg. 7864235) (an adress I think)

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So once you destroy the list, you lose the memory. L still points to that piece of memory, but now it has ... something ... in it, that is not necessarily your list. (Hence the suggestions to set L to NULL in your main, once you get back from the destroy function.) Any attempts to use that pointer after you've freed it are doomed to heartache.

  4. #19
    Registered User
    Join Date
    Mar 2010
    Posts
    31
    Quote Originally Posted by tabstop View Post
    So once you destroy the list, you lose the memory. L still points to that piece of memory, but now it has ... something ... in it, that is not necessarily your list. (Hence the suggestions to set L to NULL in your main, once you get back from the destroy function.) Any attempts to use that pointer after you've freed it are doomed to heartache.
    It works. Thanks.

  5. #20
    Registered User
    Join Date
    May 2010
    Posts
    4
    I'm working on a exact same code.

    Is it possible to get your function to point L to NULL somehow, as it is user friendly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked lists and new/delete?
    By Neo1 in forum C++ Programming
    Replies: 12
    Last Post: 08-11-2007, 10:17 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  4. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM
  5. eof in fstream & singly linked lists
    By mazo in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2002, 09:50 AM