home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!uwm.edu!ux1.cso.uiuc.edu!cs.uiuc.edu!sparc0b!ctaylor
- From: ctaylor@cs.uiuc.edu (Conrad Taylor)
- Subject: What's wrong with this code...???
- Message-ID: <BzqCsy.1v4@cs.uiuc.edu>
- Sender: news@cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- Date: Wed, 23 Dec 1992 20:55:45 GMT
- Lines: 124
-
-
- Question: why would the 'free(p)' in 'main' cause this program to go
- into an infinite loop? I have enclosed complete source and not an abstract
- that can be viewed. Also, how would one break out of an infinite loop in
- BBorland C/C++ 3.1? Thanks in advance to all that reply to this post.
-
- ps: My e-mail is taylor1@wse.eecs.uic.edu.
-
- -------------------------------- Cut Here ------------------------------------
-
- #include <stdio.h>
- #include <stdlib.h>
-
-
-
- typedef int INT;
-
- struct NODE {
- INT info;
- struct NODE* prev;
- struct NODE* next;
- };
-
- typedef struct NODE* NODEPTR;
-
- /* Function prototypes. */
- void DoublyLinkedInsert(NODEPTR p, NODEPTR q);
- void DoublyLinkedDelete(NODEPTR p);
- void DoublyLinkedPrint(NODEPTR head);
- NODEPTR GetNewNode(void);
-
-
-
- NODEPTR GetNewNode(void)
- {
- NODEPTR newNode;
-
- newNode = (NODEPTR)malloc(sizeof(struct NODE));
- newNode->prev = newNode;
- newNode->next = newNode;
-
- return(newNode);
- }
-
-
- void DoublyLinkedInsert(NODEPTR p, NODEPTR q)
- {
- /* Insert node pointed to by p just after */
- /* node pointed to by q. */
-
- p->prev = q;
- p->next = q->next;
- q->next->prev = p;
- q->next = p;
- }
-
- void DoublyLinkedDelete(NODEPTR p)
- {
- /* Delete cell p from its doubly linked list. */
-
- p->prev->next = p->next;
- p->next->prev = p->prev;
- free(p);
- }
-
- void DoublyLinkedPrint(NODEPTR head)
- {
- NODEPTR temp = head->next;
-
- while(temp != head)
- {
-
- printf("Value: %2d\n", temp->info);
- temp = temp->next;
- }
- }
-
- 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);
- /*
- printf("\nDEBUG 1:\n head->info is %d\n", head->info);
- printf(" head->next->info is %d\n", head->next->info);
- printf(" head->next->next->info is %d\n", head->next->next->info);
- printf(" head->next->next->next->info is %d\n", head->next->next->next->info);
- */
- p = GetNewNode();
- p->info = 2;
- DoublyLinkedInsert(p, head);
- free(p); /* This is the line of code that I'm talking about above. */
- /*
- printf("\nDEBUG 2:\n head->info is %d\n", head->info);
- printf(" head->next->info is %d\n", head->next->info);
- printf(" head->next->next->info is %d\n", head->next->next->info);
- printf(" head->next->next->next->info is %d\n", head->next->next->next->info);
- */
- DoublyLinkedPrint(head);
- /*
- printf("\nDEBUG 3:\n head->info is %d\n", head->info);
- printf(" head->next->info is %d\n", head->next->info);
- printf(" head->next->next->info is %d\n", head->next->next->info);
- printf(" head->next->next->next->info is %d\n", head->next->next->next->info);
- */
-
- free(head);
- free(p);
-
- return 0;
- }
-
-
-
-
-
-