alloca() portability

Leslie Mikesell les at chinet.chi.il.us
Sat Nov 10 10:35:27 AEST 1990


In article <27537 at mimsy.umd.edu> chris at mimsy.umd.edu (Chris Torek) writes:

>All in all, I would say that the threat of failure (from optimizing
>compilers) plus that of nonportability (to environments where not even
>a mallocking alloca can be used) far outweigh the convenience of
>not having to call free().  All that is left is the difference in
>speed, and there are simple ways around that problem as well.

The usual "simple" way goes something like this:

#define BIG_BUFF 1024  /* that should be big enough */

work(str)
char *str;
{
   buffer[BIG_BUFF);
   strcpy(buffer,str); /*  make a local copy to manipulate */
   ....
}

This does handle recursion and longjmp()'s out of signal handlers with
a lot less work than a malloc() based solution and it could even be
kept from crashing at random if a length check were added, but there
is no guarantee that an entire string can be manipulated.  It just
leaves the nagging feeling that something necessary was omitted from
the language....

The argument that alloca isn't a requirement because it's little-used
(and it's little-used because its not required) doesn't help much
either.

Les Mikesell
  les at chinet.chi.il.us



More information about the Comp.lang.c mailing list