home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / c / 12221 < prev    next >
Encoding:
Text File  |  1992-08-12  |  2.2 KB  |  54 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!gumby!wupost!csus.edu!netcom.com!qualtrak
  3. From: qualtrak@netcom.com (Qual Trak)
  4. Subject: Re: general tree
  5. Message-ID: <6t4myyq.qualtrak@netcom.com>
  6. Date: Wed, 12 Aug 92 15:05:40 GMT
  7. Organization: QualTrak Corporation
  8. References: <1992Aug11.220049.861@ugle.unit.no> <chuckb.713584418@milton> <1992Aug12.092635.27271@ugle.unit.no>
  9. Lines: 43
  10.  
  11. In article <1992Aug12.092635.27271@ugle.unit.no> bjornmu@idt.unit.no (Bj|rn P. Munch) writes:
  12. >In article <chuckb.713584418@milton>, chuckb@milton.u.washington.edu (Chuck Bass) writes:
  13. >|> bjornmu@idt.unit.no (Bj|rn P. Munch) writes:
  14. >|> 
  15. >|> >In article <1992Aug11.200208.19716@infonode.ingr.com>, dpm@wizard.b29.ingr.com (David Mikesell) writes:
  16. >|> >|> 
  17. >|> >|> I am beginning work on a C program that will manipulate a general tree, i.e.
  18. >|> >|> a tree in which each node can have an arbitrary number of children.  An ex-
  19. >|> >|> ample declaration of the structure follows:
  20. >|> >|> 
  21. >|> >|> struct node
  22. >|> >|> {
  23. >|> >|>     DATA        data;
  24. >|> >|>     struct node *parent;
  25. >|> >|>     struct node **children;
  26. >|> >|> };
  27. >|> 
  28. >|> >In stead of putting the children into an array (what do you do if this
  29. >|> >array suddenly isn't big enough?), you may consider putting them in a
  30. >|> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  31. >|> This is a perfect time to use something like ... realloc.
  32. >
  33. >I also thought about realloc(), but soon realized this would be a
  34. >rather bad idea.  Maybe I should have explained why...
  35. >
  36. >Remember: realloc() may move your array to another address.  If you
  37. >have references to the children that were stored there (like parent
  38. >pointers in _their_ children), then you're in deep trouble.
  39. >
  40.  
  41. Seems to me realloc () will work quite adequately if you reference the
  42. children pointer explicitly thru the node - i.e.
  43.     struct node *nodeptr, (nodeptr1;
  44.  
  45.     nodeptr1 = nodeptr->children [0];
  46.  
  47. Will always return you the same value even if nodeptr->children has been
  48. realloced ();
  49.  
  50. ------------------------------------------------------------------------
  51. John Birchfield - QualTrak Corp (408) 730-2674 Home (408) 736-3852
  52. jb@QualTrak.COM
  53. ------------------------------------------------------------------------
  54.