[comp.lang.c] extensible arrays

chris@mimsy.UUCP (03/01/87)

Following much ado about extensible arrays inside structures, e.g.,

	struct string {
		int	s_len;		/* actual length */
		int	s_space;	/* space allocated */
		char	s_text[0];	/* really longer */
	};

	...
	/* alloc is a malloc front end that aborts on failure */
	s = (struct string *) alloc(sizeof (*s) + length);
	s->s_len = length;
	s->s_space = length;
	...

in article <1222@dg_rtp.UUCP> meissner@dg_rtp.UUCP (Michael Meissner) writes:
>yea, and malloc will also add anywhere of 0-64 extra bytes in the
>memory allocation.  Extreme micro optimizations like this can backfire.

True, but it is worth pointing out that if your malloc has a constant
rounding, this wins; and even if it has a variable rounding, if the
rounding is proportionate to the size of the object being allocated
and the overhead plus rouding for the `normalised' structure (here
2*sizeof(int) + sizeof(char *)) is larger than the average rounding
for the extensible version, the extensible version still wins.  It
may even be possible to determine this at run time, and self-configure.

(It is also worth pointing out that this takes a lot of effort.)
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP:	seismo!mimsy!chris	ARPA/CSNet:	chris@mimsy.umd.edu