home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!wupost!m.cs.uiuc.edu!sunb10.cs.uiuc.edu!sparc10.cs.uiuc.edu!pjl
- From: pjl@sparc10.cs.uiuc.edu (Paul Lucas)
- Subject: Re: dynamic arrays of objects
- Message-ID: <1992Sep15.030223.13792@sunb10.cs.uiuc.edu>
- Keywords: arrays, dynamic
- Sender: news@sunb10.cs.uiuc.edu
- Organization: University of Illinois at Urbana-Champaign
- References: <5515@naucse.cse.nau.edu>
- Distribution: usa
- Date: Tue, 15 Sep 1992 03:02:23 GMT
- Lines: 34
-
- In <5515@naucse.cse.nau.edu> jdc@naucse.cse.nau.edu (John Campbell) writes:
-
- >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.
-
- [ ...stuff elided... ]
-
- >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?
-
- *****> There's no such thing as a free lunch. Even if realloc were to
- exist, it would have to do the work (you don't want to).
-
- For simple things: alloc a bigger array, copy old into new,
- delete old. Period. Often, it's done in "chunks" so you don't
- have to keep alloc'ing every time you add just _one_ element.
-
- See also:
-
- Thomas Cargil. "A Dynamic Vector is Harder Than It
- Looks." C++ Report, vol. 4, no. 5, June 1992.
- pp. 47-50.
- --
- - Paul J. Lucas University of Illinois
- AT&T Bell Laboratories at Urbana-Champaign
- Naperville, IL pjl@cs.uiuc.edu
-