Mail Archives: djgpp/1996/08/13/06:35:20
Eli Zaretskii wrote:
> But I don't recommend using it for allocations larger than, say 5KB.
> That is because your function might be called in a deeply nested program,
> when some (most) of the stack is already used up. Think about `alloca'
> as a way to declare a variable-size local array: usually, when the array
> size gets large, you'd prefer using `malloc' or declare it as a static.
> The same logic applies to `alloca' IMHO.
Agree : avoid alloca() if your program has pronounced recursive
functions... and ban it for large data chunks.
The main interest of alloca() is its speed, and the fact you do not need
to bother about freeing. However, its is a bit less safe than malloc().
There are many programs, which stack never is much nested (no recursive
functions), but which allocate many small chunks of memory. In this case
alloca() can be a good alternative, and end up in significant speed
improvements.
BTW, do you think it would be possible for a compiler to optimise the use
of memory (i.e. allocate in the stack whenever it is safe and possible,
and in the heap in other time). Comments anyone ?
Francois
- Raw text -