home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!netnews!bandy
- From: bandy@netnews.jhuapl.edu (Mike Bandy)
- Subject: Re: What's wrong with this code...???
- Message-ID: <Bzz4MH.G8v@netnews.jhuapl.edu>
- Organization: JHU/Applied Physics Laboratory
- References: <BzqCsy.1v4@cs.uiuc.edu>
- Date: Mon, 28 Dec 1992 14:37:29 GMT
- Lines: 44
-
- ctaylor@cs.uiuc.edu (Conrad Taylor) writes:
-
-
- > Question: why would the 'free(p)' in 'main' cause this program to go
- >into an infinite loop?
-
- The code all looks good, but you're free()ing p twice. Also, once
- p is free'ed, your linked list continues to point to the memory that
- was dealloced - that's illegal. You should remove p from the
- linked list, then deallocate p *once*.
-
- >-------------------------------- Cut Here ------------------------------------
-
- >INT main(void)
- >{
- > NODEPTR head;
- > NODEPTR p;
-
- > head = (NODEPTR)malloc(sizeof(struct NODE));
- > head->prev = head;
- > head->next = head;
- > head->info = -2;
-
- > p = GetNewNode();
- > p->info = 1;
- > DoublyLinkedInsert(p, head);
-
- > p = GetNewNode();
- > p->info = 2;
- > DoublyLinkedInsert(p, head);
- > free(p); /* This is the line of code that I'm talking about above. */
-
- > DoublyLinkedPrint(head);
-
- > free(head);
- > free(p);
-
- > return 0;
- >}
- --
-
- Mike Bandy
- bandy@aplcomm.jhuapl.edu
- Johns Hopkins University / Applied Physics Lab
-