Daniel Stenberg <[EMAIL PROTECTED]> writes:

> On 18 Jun 2001, Hrvoje Niksic wrote:
> 
>> There are good reasons to use alloca instead of malloc
> 
> I know this is taken out of context, but I'm curious on this. What
> are the good technical reasons to use alloca instead of malloc?

* Reduced memory fragmentation -- you don't need to worry about
  thousands of small mallocs fragmenting your heap, alloca always
  allocates and flawlessly frees stuff off the stack.

* Easier coding -- you don't need to worry about freeing the value
  before you return from the function.

* Faster resulting code -- alloca generates the code that simply moves
  the stack pointer.  malloc on the other hand is a genuine function
  call which needs to search the heap for a "good match", resize the
  heap if necessary, handle freeing, and be all around as fast as a
  rabbit and as memory-efficient as the proverbial Scott with his
  money, all at the same time.  Obviously, one of these has to suffer.

> Aren't those occurences just giving you headache when porting wget
> to platforms that don't offer alloca()?

First, this problem has *never* occurred so far, and Wget has been
compiled on some very weird platforms.  Second, the standard file
`alloca.c' distributed with Wget serves for exactly that purpose.
BTW, are you aware that popular (and portable!) GNU programs such as
GNU Emacs use alloca all over?  If there were non-obvious significant
problems with hit, they would have been discovered by now.

So far, the amount of headache has been 0, whereas the benefits have
been significant.

Reply via email to