alloca() for TurboC

R. Kym Horsell vu0310 at bingvaxu.cc.binghamton.edu
Sat Aug 18 23:51:21 AEST 1990


In article <2745 at onion.reading.ac.uk> ssuhook at susssys1.cs.reading.ac.uk writes:
\\\
>In summary, I am mainly interested in finding out how to get an alloca() for
>the PC for Turbo C, and also what it is that alloca() actually does.
\\\

Alloca does something *like*

char *alloca(n) {
	n =			/* round up to words */;
	((char*)SP) += n;	/* bump up the stack ptr */
	return (char*)SP-n;	/* return address of stuff on stack */
	}

I.e. it just bumps up the stack by n bytes (usually word alignining
the result) and returns the address of the new stack section.
Since the SP is restore by C exit stuff from a `frame ptr register'
(typically, anyway) this memory is deallocated at the end of the function.

Note I've assumed the stack grows up (not a normal thing these days).
Also we can't *actually* write alloca() as above even when you can
refer to the SP directly since its exit will deallocate the
space just allocated -- you should think of alloca() as written
above as a macro.

To solve your problem with proting Bison -- why not use
(a)	malloc/alloc and just forget deallocating it, or
(b)	malloc/alloc and use an explicit free at the end of
	the function its used it (might as well catch *all*
	returns from the function while you're about it).

Ive written a fairly complete Yacc/Lex for AT's and didn't
both about memory leakage. After a PDP-11 there was more memory
than I new what to do with (ahhh...many years ago now).

Hope this's been some help,
-Kym Horsell



More information about the Comp.lang.c mailing list