Article View: comp.lang.c.moderated
Article #2634Re: Legal alloca?
From: John Cochran
Date: Sat, 20 Apr 1996 19:17
Date: Sat, 20 Apr 1996 19:17
41 lines
2764 bytes
2764 bytes
In article <4l880s$75k@solutions.solon.com>, <news@Thinkage.On.CA> wrote: >In article <4l33l8$7of@solutions.solon.com> mfg@ee.edinburgh.ac.uk (Michael F Gordon) writes: >>Another place where I've found alloca() very useful is to allocate >>memory in a recursive descent parser for a simple calculator. >>Instead of cluttering the code with error checks at every step and >>having to free memory and pass error values back up the calling >>chain, I just longjmp'd back to the top level parse() function >>whenever one of the low level routines found an error. >> >>So, just how non-portable and horrible is this? >> >Different implementation have varing "flavours" of alloca >1) It may not exist. It is after all NOT required by the C standard. > You clearly are not portable to these implementations. >2) It exists and works by playing games with the stack frame size. > Your technique will not work on implementations where there > are tight limits on frame size or total stack space. > Fixing a maximum total stack at compile/link time is not > uncommon. And being able to extend the function framesize > dynamically usually requires the calling sequence to sacrifice > a register. >3) It exists, but works by calling malloc(), and arranging a hidden > free() on return. Your technique will only work on those > implementations where longjmp() also looks after these hidden > free() calls. > >How portable do you want to be? What range of machines do you want >to support? How about 4) It exists as a normal function that uses malloc. Every block of memory allocated is placed in a linked list control structure that has a copy of the stack pointer at the time of call. Whenever alloca is called, it first frees all memory blocks that have a recorded stack pointer (larger or smaller depending on direction of stack growth) than the current stack pointer. This method doesn't immediately free memory allocated by alloca(), but does keep the number of blocks needing to be released fairly small. This method is used by the GNU alloca() function supplied with some of the GNU programs. John Cochran
Technical Details
Message-ID:
<4lbd7t$m27@solutions.solon.com>Path:
arc1-ca.pugleaf.net!solon.com!not-for-mailReferences:
<4koe4u$8sl@solutions.solon.com> <4kr7b2$kcb@solutions.solon.com> <4l33l8$7of@solutions.solon.com> <4l880s$75k@solutions.solon.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!solon.com!not-for-mail From: jdc@access.digex.net (John Cochran) Newsgroups: comp.lang.c.moderated Subject: Re: Legal alloca? Date: 20 Apr 1996 14:17:17 -0500 Organization: Express Access Online Communications, Greenbelt, MD USA Sender: clc@solutions.solon.com Approved: clc@solutions.solon.com Message-ID: <4lbd7t$m27@solutions.solon.com> References: <4koe4u$8sl@solutions.solon.com> <4kr7b2$kcb@solutions.solon.com> <4l33l8$7of@solutions.solon.com> <4l880s$75k@solutions.solon.com> NNTP-Posting-Host: solutions.solon.com