Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!cs.utexas.edu!uunet!edsews!bgalli!bga From: bga@bgalli.eds.com (Billy G. Allie) Newsgroups: comp.lang.c Subject: Re: What is alloca()? [Is this unportable implementation OK?] Summary: Corrected alloca() for Turbo C 2.0 Message-ID: <417@bgalli.eds.com> Date: 4 Oct 89 01:58:35 GMT References: <3823.2518c141@uwovax.uwo.ca> <3839.251f4ecf@uwovax.uwo.ca> Organization: BGA's home system, Dearborn, MI. Lines: 50 In article <3839.251f4ecf@uwovax.uwo.ca>, 2014_5001@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. -------------------------------><------------------------------- # 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 -------------------------------><------------------------------- -- ____ | Billy G. Allie | Internet..: bga@bgalli.eds.com | /| | 7436 Hartwell | UUCP......: uunet!{mcf|edsews}!bgalli!bga |-/-|----- | Dearborn, MI 48126 | Compuserve: 76337,2061 |/ |LLIE | (313) 582-1540 | Genie.....: BGALLIE