[comp.lang.c] Alloca Wars: Another View

bitbug@vsi1 (James Buster) (08/09/88)

There is one reason to use alloca: longjmp. Code in which it is
possible to longjmp out of may not work when using malloc as
the memory allocator of choice, since the corresponding free
may never be called, wasting memory and perhaps causing your
program to crash. Longjmp works by restoring a previous stack
frame, and deleting the present one (which includes any memory
allocated by alloca). Thus, programs which tend to use longjmp
a lot (shells, text editors, etc) will probably work better and
more reliably with alloca than malloc.
-- 
-- 

ok@quintus.uucp (Richard A. O'Keefe) (08/09/88)

In article <895@vsi1.UUCP> bitbug@vsi1 (James Buster) writes:
>There is one reason to use alloca: longjmp.

There are a lot of other things one might want to clean up as well,
such as closing and unlinking scratch files, free()ing malloc()ed
blocks which _couldn't_ be allocated with alloca() because they
were to be returned to the caller if all went well, ...

The answer is to put together something like Lisp's (unwind-protect ...),
not to introduce special alloca(), fopena(), &c functions.  In an editor
(one of Buster's examples) you might well want to undo changes to the
buffer made since the transaction started.  And so it goes.  Freeing
memory blocks which can only be used locally is the least of one's worries.

Now, what's a good way to do something like (unwind-protect ...) in C?
[And people wonder why I read comp.lang.ada...]