alloca() for TurboC

Alex Pruss pruss at ria.ccs.uwo.ca
Tue Aug 28 03:22:28 AEST 1990


In article <1990Aug22.032243.18214 at usenet.ins.cwru.edu> trier at po.CWRU.Edu writes:
>Here's a version of alloca I use.  It's my clone of a version from some
>piece of PD software I saw a year or two ago.
>
>        #define alloca(A) ((_SP -= ((A)+1) & 0xFFFE), (void far *)(((unsigned long)_SS << 16) | _SP))

This looks surprisingly like the original of my alloca().  Unfortunately it
doesn't work.  (Not at all!--my version didn't either--it took many hours
of painful debugging to find what was wrong.)  You see it returns SS:SP.
However, next time a function call, push, or interrupt comes along, a word
gets stored at SS:SP which is part of the allocated area.  Thus a minimal
fix is to change it to:

#define alloca(A) ((_SP -= ((A)+1) & 0xFFFE), (void far *)(((unsigned long)_SS << 16) | _SP+2))

Unfortunately this fails in the case that the function has stored DS/SI/DI
on the stack.  Furthermore one must beware that the calling function may not
have a proper stack frame.

>I should add the disclaimer that I'm not entirely sure that this works.
>It's my own version of someone else's, but I changed it a bit.  (I don't
>remember what the original looked like.)
It was less elegant in the forcing of A to be even, and it too didn't work.
It included also a { char dummy; dummy=dummy; } type of thing to force a
stack frame.

Anyways, the latest WORKING (I hope...  I tested it...) version I've just posted
to alt.sources.  (The whole thing with docs and uuencoded object files is
13Kb, so I didn't want to post it here).

pruss at uwo.ca // internet
pruss at uwovax // bitnet
pruss at ria    // uucp
//\\ The SCHSPH Guy //\\



More information about the Comp.lang.c mailing list