What is alloca()? [Is this unportable implementation OK?]

2014_5001 at uwovax.uwo.ca 2014_5001 at uwovax.uwo.ca
Wed Sep 27 00:26:22 AEST 1989


In article <3823.2518c141 at uwovax.uwo.ca>, 2014_5001 at uwovax.uwo.ca writes:
> I was trying to compile FLEX with TurboC 2.0.  Not having a yacc, I
> ran bison on parse.y.
> 
> With a minor change in the source, I managed to get the whole thing
> compiled.  Unfortunately, it requests the presence of an alloca()
> routine, apparently with a prototype like:
>   void *alloca(size_t s);
> (As well as I can tell from the context and the type casting).
> It does not appear to figure in parse.y, so it appears likely to me
> that it is put in by my bison.  What does alloca() do?
> It does not appear in the TurboC 2.0 library, nor in Unix SysV
> man pages.

I rarely follow up on my own articles, but here goes..

I have used the following alloca() implementation for the ibm pc
(highly unportable):

----- alloca.h----
static unsigned int _INtERNAL_alloca_;
#define alloca(x) \
  ( _INtERNAL_alloca_=_SP, _SP-=(x), (void *)_INtERNAL_alloca_ )
-----

I am wondering whether this does what alloca() should do.
Have I missed something?

Turbo C apparently allocates stack space for all the blocks in
a function once at the top.  i.e. given
void f(void) { int a; { int b; } },
space for both a and b will be allocated at the top of the function,
and deallocated upon return.  This means that since my alloca() macro
relies on the automatic var deallocation, the space will be in use
until a return.

-- 
Alexander Pruss, at one of: Department of Applied Mathematics, Astronomy,
Mathematics, or Physics                     University of Western Ontario 
pruss at uwovax.uwo.ca         pruss at uwovax.BITNET          A5001 at nve.uwo.ca 



More information about the Comp.lang.c mailing list