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

Billy G. Allie bga at bgalli.eds.com
Wed Oct 4 11:58:35 AEST 1989


In article <3839.251f4ecf at uwovax.uwo.ca>, 2014_5001 at uwovax.uwo.ca writes:
> 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?

The macro you gave above will fail in any of the large data models.  Also,
your macro could leave the stack pointer pointing to an odd address, some-
thing that the 80x86 processors do not like.  I am including the source for
the macro that I wrote when I ported BISON to the MS-DOS operating system
and the Turbo-C compiler.  It will work correctly for any data model.

-------------------------------><Cut Here><-------------------------------
# To unbundle, sh this file
echo alloca.h 1>&2
cat >alloca.h <<'End of alloca.h'
/***********************************************************************\
| The following gives the definition of a macro (alloca) that allocates	|
| space like malloc EXCEPT that it is allocated from the stack and does	|
| not have to be freed.  The allocated space will automatically disap-	|
| pear when the function that allocated the space exits.		|
|									|
|			      W*A*R*N*I*N*G				|
| This code is VERY compiler dependant and will only work for Turbo C	|
| versions 1.5 and 2.0.  You are forwarned.				|
|									|
| This code is released into the public domain.				|
\***********************************************************************/

#if !defined(alloca)
#if defined(__COMPACT__) || defined(__LARGE__) || defined(__HUGE__)
#define alloca(i) (_SP-=((i+1)&0xFFFE),(void *)(((unsigned long)_SS<<16)|_SP))
#else
#define alloca(i) (_SP-=((i+1)&0xFFFE),(void *)_SP)
#endif
#endif
End of alloca.h
-------------------------------><Cut Here><-------------------------------
-- 
____	   | Billy G. Allie	| Internet..: bga at bgalli.eds.com
|  /|	   | 7436 Hartwell	| UUCP......: uunet!{mcf|edsews}!bgalli!bga
|-/-|----- | Dearborn, MI 48126	| Compuserve: 76337,2061
|/  |LLIE  | (313) 582-1540	| Genie.....: BGALLIE



More information about the Comp.lang.c mailing list