use of alloca ?

Christopher Smith cbsmith at
Fri Jan 29 22:31:53 PST 1999

On Sat, 30 Jan 1999, Prashanth K.S. wrote:
> Hi,
>   Is alloca (in "virtualMachine") used only to get some memory that is 
> freed automatically on function exit ? or to get memory only from the 
> stack ? 
>   can someone please explain the need for this call ?
>   can alloca be replaced by malloc (& free before function exit). If not, 
> can something else be used instead of it to achieve the same effect ?
Alloca, if you check your man pages, is basically malloc except that it
allocates memory n the stack instead of in the heap. As you described,
this has the advantage that the memory is freed on exit. The benefits
extend beyond this though. Heaps can get fragmented, particularly if you
do lots of short term allocations. Stack allocation avoids these problems.

So, it's a performance optimization, but it's a good idea to use it
wherever possible.


More information about the kaffe mailing list