bug-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using alloca


From: Hans Aberg
Subject: Re: Using alloca
Date: Thu, 14 Dec 2000 15:31:09 +0100

At 14:03 +0100 0-12-14, Akim Demaille wrote:
>Hans> What is the advantage of using alloca over malloc?
>
>Plenty!
...

Thank you for your detailed reply about the use of alloca.

I also received the following excellent description by Florian Krohm, but
sorrily enough, he didn't cc the Bison list (sorry Florian if you get upset
by me making your letter public!):

At 08:30 -0500 0-12-13, Florian Krohm wrote:
>Memory allocated with alloca is allocated on the stack.
>This is usually a cheap operation much cheaper than
>going through malloc. Also, when you return from a
>function that allocated some memory using alloca, that
>dynamic memory is automatically freed since the
>stack frame for that function gets thrown away. You
>can argue that this is convenient. You could also argue that
>is encourages sloppy coding practices.. You decide.
>
>As far as I know alloca is not part of POSIX, however it
>is part of X/Open. So it might not be available everywhere..

Akim Demaille wrote:
>   The prototype for `alloca' is in `stdlib.h'.  This function is a BSD
>extension.

On my computer it is in <alloca.h>. If it is found in <stdlib.h>, that C
compiler will probably not conform to the C ISO-IEC-ANSI standard.

>Advantages of `alloca'
>......................
>   Here are the reasons why `alloca' may be preferable to `malloc':
>
>   * Using `alloca' wastes very little space and is very fast.  (It is
>     open-coded by the GNU C compiler.)
>
>   * Since `alloca' does not have separate pools for different sizes of
>     block, space used for any size block can be reused for any other
>     size.  `alloca' does not cause memory fragmentation.

The reason malloc & C++ operator new() often are slow is often that they
are poorly written. For example, a way to avoid memory fragmentation with
malloc is to only allocate block of size 2^n in separate sections, one for
each n. I guess alloca is still going to be faster though.

>   * Nonlocal exits done with `longjmp' (*note Non-Local Exits::)
>     automatically free the space allocated with `alloca' when they exit
>     through the function that called `alloca'.  This is the most
>     important reason to use `alloca'.

The way to do it under C++ is to wrap up & delete in a class and then to
use exceptions instead of longjump. Then it makes no difference if one is
using operator new or alloca.

>Disadvantages of `alloca'
>.........................
>
>   These are the disadvantages of `alloca' in comparison with `malloc':
>
>   * If you try to allocate more memory than the machine can provide,
>     you don't get a clean error message.  Instead you get a fatal
>     signal like the one you would get from an infinite recursion;
>     probably a segmentation violation (*note Program Error Signals::).

I surmise that even if the OS knows how to enlarge the stack, allocating
large chunks with alloca might be slow, as the stack then must be enlarged.

>   * Some non-GNU systems fail to support `alloca', so it is less
>     portable.  However, a slower emulation of `alloca' written in C is
>     available for use on systems with this deficiency.

I gather one must have a function that is called when the function calling
alloca expires. In C++ that is not so difficult (just allocate an automatic
variable, and use its "delete"), but I wonder how to it in C.

Also add the alloca disadvantage
  * alloca cannot be used if the dynamic allocation should live longer
    than the function it was called from.


  Hans Aberg





reply via email to

[Prev in Thread] Current Thread [Next in Thread]