[comp.unix.wizards] alloca vs var-len auto arrays

andrew@frip.wv.tek.com (Andrew Klossner) (04/27/89)

> Are there uses of alloca() that can't be handled by variable-length
> automatic arrays:
> 	{ int n = 10; { char s[n]; } }
> which are allowed by GNU cc?

Sure:

doit() {

	while (getting_list) {
		next_list_element = alloca(...);
		sort next_list_element into list
	}

	for (each element in list) {
		operate on element;
	}

	return;	/* free all those alloca's */
}

> Are there architectures that will not allow you to implement this?

No, with compiler assist you can always implement it; but its
complexity can approach that of marking all mallocs and teaching the
compiler (and longjmp) to call un_malloc_temporaries on each procedure
return.

  -=- Andrew Klossner   (uunet!tektronix!orca!frip!andrew)      [UUCP]
                        (andrew%frip.wv.tek.com@relay.cs.net)   [ARPA]

kjones@talos.UUCP (Kyle Jones) (05/03/89)

>> Are there architectures that will not allow you to implement this?
>
> No, with compiler assist you can always implement it; but its
> complexity can approach that of marking all mallocs and teaching the
> compiler (and longjmp) to call un_malloc_temporaries on each procedure
> return.

It suffices to call un_malloc_temporaries at the end of functions that
actaully use alloca, and just after successful longjmp calls.  Even
without help from the compiler the wrappers are easy to implement and
portable.  But it's akin to having to explicitly reclaim space used by
local variables.  The implementation details may be different but the
concept is the same.