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

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!spool.mu.edu!yale.edu!ira.uka.de!smurf.sub.org!easix!bc3!ktf
  3. From: ktf@bc3.GUN.de (Klaus ter Fehn)
  4. Subject: Re: What's wrong with this code...???
  5. Organization: private
  6. Date: Thu, 24 Dec 1992 15:06:46 GMT
  7. Message-ID: <BzrrBB.2MG@bc3.GUN.de>
  8. References: <BzqCsy.1v4@cs.uiuc.edu>
  9. Lines: 96
  10.  
  11.  
  12. >Question: why would the 'free(p)' in 'main' cause this program to go
  13. >into an infinite loop?  I have enclosed complete source and not an abstract
  14. >that can be viewed.  Also, how would one break out of an infinite loop in
  15. >BBorland C/C++ 3.1? Thanks in advance to all that reply to this post.
  16.  
  17. Question: Why do you free memory that contains data which is linked to your
  18.       linked-list ?
  19.  
  20.    (...)
  21.  
  22.    p = GetNewNode();
  23.    p->info = 2;
  24.    DoublyLinkedInsert(p, head);
  25.    free(p); /* This is the line of code that I'm talking about above. */
  26.         /* me too :-) */
  27.  
  28.    (...)
  29.  
  30.    Let's see, if I understand your program:
  31.  
  32.    you have a pointer
  33.  
  34.      p
  35.    ,---.
  36.    |   |
  37.    `---'
  38.  
  39.    you call GetNewNode() to allocate memory for informations (here: 2)
  40.  
  41.      p        SOMEWHERE
  42.    ,---.       ,-----------.
  43.    | o-+------>|     2     |
  44.    `---'       +-----+-----+
  45.                |     |     |
  46.            `-----+-----'
  47.  
  48.    after doing so, you insert the structure into your list:
  49.  
  50.      q        
  51.    ,---.       ,-----------.
  52.    | o-+------>|     ?     |
  53.    `---'       +-----+-----+
  54.                |  o  |     |
  55.            `--+--+-----' _
  56.                   |         |\
  57.      p           \|/          |
  58.    ,---.       ,-----------.  |
  59.    | o-+------>|     2     |  |
  60.    `---'       +-----+-----+  |
  61.                |  o  |  o--+--'
  62.            `--+--+-----' _
  63.                   |         |\
  64.                  \|/          |
  65.                ,-----------.  |
  66.                |     ?     |  |
  67.                +-----+-----+  |
  68.                |     |  o--+--'
  69.            `-----+-----'
  70.  
  71.    and the, you free the memory pointed to by 'p':
  72.  
  73.               
  74.                ,-----------.
  75.                |     ?     |
  76.                +-----+-----+
  77.                |  o  |     |
  78.            `--+--+-----'
  79.                   |
  80.      p           \|/
  81.    ,---.                       
  82.    | o-+------>  UNDEFINED
  83.    `---'          MEMORY
  84.                  CONTENTS!
  85.                          _
  86.                             |\
  87.                               |
  88.                ,-----------.  |
  89.                |     ?     |  |
  90.                +-----+-----+  |
  91.                |     |  o--+--'
  92.            `-----+-----'
  93.  
  94.    WHY ?!?
  95.  
  96. Merry X-Mas!
  97. -- 
  98. Klaus ter Fehn        <ktf@bc3.GUN.de>
  99. Neanderstr. 4        {mcshh,smurf,unido}!easix!bc3!ktf
  100. 4000 Duesseldorf 1
  101. FRG / Germany        Tel.: +49-211-676331
  102. -- 
  103. Klaus ter Fehn        <ktf@bc3.GUN.de>
  104. Neanderstr. 4        {mcshh,smurf,unido}!easix!bc3!ktf
  105. 4000 Duesseldorf 1
  106. FRG / Germany        Tel.: +49-211-676331
  107.