Stretchy arrays (actually stretchy strings)

David Collier-Brown daveb at geaclib.UUCP
Sun Nov 6 02:26:22 AEST 1988


 From article <1709 at garth.UUCP>, by smryan at 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



More information about the Comp.lang.c mailing list