home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / lang / cplus / 13637 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  2.7 KB

  1. Path: sparky!uunet!cis.ohio-state.edu!zaphod.mps.ohio-state.edu!wupost!ukma!asuvax!ncar!noao!arizona!naucse!jdc
  2. From: jdc@naucse.cse.nau.edu (John Campbell)
  3. Newsgroups: comp.lang.c++
  4. Subject: dynamic arrays of objects
  5. Keywords: arrays, dynamic
  6. Message-ID: <5515@naucse.cse.nau.edu>
  7. Date: 14 Sep 92 21:20:48 GMT
  8. Organization: Northern Arizona University, Flagstaff AZ
  9. Lines: 47
  10.  
  11. Well, I'm about stumped.  As a struggling new c++ programmer I can only
  12. guess that I'm having a paradigm shift problem.  
  13.  
  14. What I want to do is increase an array of pointers to objects so that 
  15. I can write an ``append'' member function.  I've read question 35 of 
  16. the FAQ and so I know that realloc is not a good thing.  However, I 
  17. want to simply increase the size of the array of pointers and (as a
  18. C programmer) it seems to me that this should be easy to do.
  19.  
  20. Here's two examples of the problem I'm facing--they're the same in
  21. my mind.
  22.  
  23. class Timed_list:                          class row_vector:
  24.    int count;                                 int dimen;
  25.    Timed_elem *array;                         Fraction array;
  26.    . . .                                   . . .
  27.  
  28. I want to add a new Timed_elem to Timed_list or a new Fraction to row_vector.  
  29. I can easily increment count or dimen, and use new to build a new Fraction 
  30. or a new Timed_elem, but how in the world do I ensure that there is space 
  31. in array for the next element (array[count] = Timed_elem (item))?
  32. I need to make ``array'' grow at this point.
  33.  
  34. I realize c++ has a fine mechanism for allocating memory for arrays
  35. if you know the size or can assume a maximum size ahead of time.  However,
  36. I would rather dynamically allocate the memory and reallocate it at
  37. various stages as my arrays grow.  Anything less, in my mind, is a step
  38. backward--back to those FORTRAN days when you had to recompile to overcome
  39. arbitrary array limits in the code.
  40.  
  41. Is this normally done by "renewing" the array itself and replacing it
  42. with array[count+1] in the append routine?  Otherwise, in Scott Meyers'
  43. book (Effective C++) he shows using a linked list to solve a problem 
  44. similiar to this.  I could implment a linked list, but it seems nasty--do 
  45. I next have to overload the array operator ``[]'' simply to add a slot 
  46. to an otherwise fully functional array.  And how do I effeciently traverse 
  47. a linked list to reach the "i'th" element, anyway?
  48.  
  49. What am I missing? 
  50.  
  51. I apologize if this has been hashed out here recently.  I read the FAQ
  52. as carefully as possible and couldn't find a definitive answer there...
  53. As I said, I'm probably frustrated due to old methodology from a long
  54. time ago in a paradigm far, far away.
  55. -- 
  56.     John Campbell               jdc@naucse.cse.nau.edu
  57.                                     JDC@NAUVAX.UCC.NAU.EDU
  58.