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