home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!smurf.sub.org!easix!bc3!ktf
- From: ktf@bc3.GUN.de (Klaus ter Fehn)
- Subject: Re: What's wrong with this code...???
- Organization: private
- Date: Thu, 24 Dec 1992 15:06:46 GMT
- Message-ID: <BzrrBB.2MG@bc3.GUN.de>
- References: <BzqCsy.1v4@cs.uiuc.edu>
- Lines: 96
-
-
- >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.
-
- Question: Why do you free memory that contains data which is linked to your
- linked-list ?
-
- (...)
-
- p = GetNewNode();
- p->info = 2;
- DoublyLinkedInsert(p, head);
- free(p); /* This is the line of code that I'm talking about above. */
- /* me too :-) */
-
- (...)
-
- Let's see, if I understand your program:
-
- you have a pointer
-
- p
- ,---.
- | |
- `---'
-
- you call GetNewNode() to allocate memory for informations (here: 2)
-
- p SOMEWHERE
- ,---. ,-----------.
- | o-+------>| 2 |
- `---' +-----+-----+
- | | |
- `-----+-----'
-
- after doing so, you insert the structure into your list:
-
- q
- ,---. ,-----------.
- | o-+------>| ? |
- `---' +-----+-----+
- | o | |
- `--+--+-----' _
- | |\
- p \|/ |
- ,---. ,-----------. |
- | o-+------>| 2 | |
- `---' +-----+-----+ |
- | o | o--+--'
- `--+--+-----' _
- | |\
- \|/ |
- ,-----------. |
- | ? | |
- +-----+-----+ |
- | | o--+--'
- `-----+-----'
-
- and the, you free the memory pointed to by 'p':
-
-
- ,-----------.
- | ? |
- +-----+-----+
- | o | |
- `--+--+-----'
- |
- p \|/
- ,---.
- | o-+------> UNDEFINED
- `---' MEMORY
- CONTENTS!
- _
- |\
- |
- ,-----------. |
- | ? | |
- +-----+-----+ |
- | | o--+--'
- `-----+-----'
-
- WHY ?!?
-
- Merry X-Mas!
- --
- Klaus ter Fehn <ktf@bc3.GUN.de>
- Neanderstr. 4 {mcshh,smurf,unido}!easix!bc3!ktf
- 4000 Duesseldorf 1
- FRG / Germany Tel.: +49-211-676331
- --
- Klaus ter Fehn <ktf@bc3.GUN.de>
- Neanderstr. 4 {mcshh,smurf,unido}!easix!bc3!ktf
- 4000 Duesseldorf 1
- FRG / Germany Tel.: +49-211-676331
-