home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18927 < prev    next >
Encoding:
Text File  |  1992-12-28  |  1.3 KB  |  55 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!netnews!bandy
  3. From: bandy@netnews.jhuapl.edu (Mike Bandy)
  4. Subject: Re: What's wrong with this code...???
  5. Message-ID: <Bzz4MH.G8v@netnews.jhuapl.edu>
  6. Organization: JHU/Applied Physics Laboratory
  7. References: <BzqCsy.1v4@cs.uiuc.edu>
  8. Date: Mon, 28 Dec 1992 14:37:29 GMT
  9. Lines: 44
  10.  
  11. ctaylor@cs.uiuc.edu (Conrad Taylor) writes:
  12.  
  13.  
  14. >    Question: why would the 'free(p)' in 'main' cause this program to go
  15. >into an infinite loop?
  16.  
  17. The code all looks good, but you're free()ing p twice.  Also, once
  18. p is free'ed, your linked list continues to point to the memory that
  19. was dealloced - that's illegal.  You should remove p from the 
  20. linked list, then deallocate p *once*.
  21.  
  22. >-------------------------------- Cut Here ------------------------------------
  23.  
  24. >INT main(void)
  25. >{
  26. >   NODEPTR head;
  27. >   NODEPTR p;
  28.  
  29. >   head = (NODEPTR)malloc(sizeof(struct NODE));
  30. >   head->prev = head;
  31. >   head->next = head;
  32. >   head->info = -2;
  33.  
  34. >   p = GetNewNode();
  35. >   p->info = 1;
  36. >   DoublyLinkedInsert(p, head);
  37.  
  38. >   p = GetNewNode();
  39. >   p->info = 2;
  40. >   DoublyLinkedInsert(p, head);
  41. >   free(p); /* This is the line of code that I'm talking about above. */
  42.  
  43. >   DoublyLinkedPrint(head);
  44.  
  45. >   free(head);
  46. >   free(p);
  47.  
  48. >   return 0;
  49. >}
  50. -- 
  51.  
  52.     Mike Bandy
  53.     bandy@aplcomm.jhuapl.edu
  54.     Johns Hopkins University / Applied Physics Lab
  55.