What is alloca()? [Generated by bison from flex]

Mike Haertel mike at thor.acc.stolaf.edu
Mon Sep 25 15:43:03 AEST 1989


In article <1989Sep24.050214.13898 at utzoo.uucp> henry at utzoo.uucp (Henry Spencer) writes:
>In article <3823.2518c141 at uwovax.uwo.ca> 2014_5001 at uwovax.uwo.ca writes:
>>... What does alloca() do?
>>It does not appear in the TurboC 2.0 library, nor in Unix SysV
>>man pages.
>
>It's a nonstandard botch (originating in some poorly-designed ancient Unix
>systems) that some of the Gnoids are really fond of.  The theory is that
>it's malloc() except that the storage automatically goes away when you leave
>the function it was called in.  This is more than a little difficult to
>implement on some machines...

I'm glad you said "SOME OF the Gnoids" (emphasis mine), Henry.
Not all of us are particularly fond of alloca().

I think alloca() originated in one of the early VAX BSD's.  My guess
would be that it was invented for use implementing Franz Lisp, and
somehow wormed its way into the standard library during a lapse of
judgement on someone's part.

The practical problem with alloca() is that it cannot be implemented
on machines without a frame pointer (except by compiler hack) and
therefore is not available in all environments.

There are philosophical objections too, but I won't go into them.

Unfortunately, alloca() is sometimes necessary in programs that
call longjmp().  (longjmp(), like alloca(), is evil.  Even though
it is in the C standard.)  But two wrongs do not make a right . . .

Doug Gwyn (gwyn at smoke.brl.mil) has a semi-portable fake alloca()
written in C that people without alloca() may find useful when
installing GNU software on their machines.
-- 
Mike Haertel <mike at stolaf.edu>
``There's nothing remarkable about it.  All one has to do is hit the right
  keys at the right time and the instrument plays itself.'' -- J. S. Bach



More information about the Comp.lang.c mailing list