home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13653 < prev    next >
Encoding:
Text File  |  1992-09-14  |  1.9 KB  |  48 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!wupost!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc10.cs.uiuc.edu!pjl
  3. From: pjl@sparc10.cs.uiuc.edu (Paul Lucas)
  4. Subject: Re: dynamic arrays of objects
  5. Message-ID: <1992Sep15.030223.13792@sunb10.cs.uiuc.edu>
  6. Keywords: arrays, dynamic
  7. Sender: news@sunb10.cs.uiuc.edu
  8. Organization: University of Illinois at Urbana-Champaign
  9. References: <5515@naucse.cse.nau.edu>
  10. Distribution: usa
  11. Date: Tue, 15 Sep 1992 03:02:23 GMT
  12. Lines: 34
  13.  
  14. In <5515@naucse.cse.nau.edu> jdc@naucse.cse.nau.edu (John Campbell) writes:
  15.  
  16. >What I want to do is increase an array of pointers to objects so that 
  17. >I can write an ``append'' member function.  I've read question 35 of 
  18. >the FAQ and so I know that realloc is not a good thing.  However, I 
  19. >want to simply increase the size of the array of pointers and (as a
  20. >C programmer) it seems to me that this should be easy to do.
  21.  
  22. [ ...stuff elided... ]
  23.  
  24. >Is this normally done by "renewing" the array itself and replacing it
  25. >with array[count+1] in the append routine?  Otherwise, in Scott Meyers'
  26. >book (Effective C++) he shows using a linked list to solve a problem 
  27. >similiar to this.  I could implment a linked list, but it seems nasty--do 
  28. >I next have to overload the array operator ``[]'' simply to add a slot 
  29. >to an otherwise fully functional array.  And how do I effeciently traverse 
  30. >a linked list to reach the "i'th" element, anyway?
  31.  
  32. *****>    There's no such thing as a free lunch.  Even if realloc were to
  33.     exist, it would have to do the work (you don't want to).
  34.  
  35.     For simple things: alloc a bigger array, copy old into new,
  36.     delete old.  Period.  Often, it's done in "chunks" so you don't
  37.     have to keep alloc'ing every time you add just _one_ element.
  38.  
  39.     See also:
  40.  
  41.         Thomas Cargil.  "A Dynamic Vector is Harder Than It
  42.             Looks."  C++ Report, vol. 4, no. 5, June 1992.
  43.             pp. 47-50.
  44. -- 
  45.     - Paul J. Lucas                University of Illinois    
  46.       AT&T Bell Laboratories        at Urbana-Champaign
  47.       Naperville, IL            pjl@cs.uiuc.edu
  48.