Path: utzoo!yunexus!geac!geaclib!daveb From: daveb@geaclib.UUCP (David Collier-Brown) Newsgroups: comp.lang.c Subject: Re: Stretchy arrays (actually stretchy strings) Message-ID: <3387@geaclib.UUCP> Date: 5 Nov 88 16:26:22 GMT Article-I.D.: geaclib.3387 References: <956@goofy.megatest.UUCP> Organization: GEAC Computers, Toronto, CANADA Lines: 28 From article <1709@garth.UUCP>, by smryan@garth.UUCP (Steven Ryan): >> Well, gee, how do you allocate an indefinite sized array? > > Glad you asked. I stuff it down a stack, each element individually > handcrafted with malloc, until I've reached the end and know exactly > how big it is. Then, IF I need random access, I copy it to an array. > Linear time. A variant on that is to write a recursive function which declares a fixed-size buffer, fills that and then recurses. On the way "up" it mallocs a single correct-size buffer and copues each chunk into it. This mitigates some of the problems mentioned by djones @ megatest.UUCP (Dave Jones). I posted a getline function some months ago that did just that. Needless to say, this works best when 1) the variation of the maximum size is only an order of magnitude or so, and 2) you start with a largish size for the declared buffer. --dave c-b -- David Collier-Brown. | yunexus!lethe!dave Interleaf Canada Inc. | 1550 Enterprise Rd. | HE's so smart he's dumb. Mississauga, Ontario | --Joyce C-B