[En-Nut-Discussion] Use of alloca in Nut/OS

Harald Kipp harald.kipp at egnite.de
Mon Aug 11 18:35:44 CEST 2008


Alain M. wrote:
> In most cases alloca() can simply be raplaces by simple local variable. 
> In fact, alloca is only usefull if variable data will be stored, which 
> is never a good thing for embedded.

The intention is to allocate no more space then actually needed during 
runtime. Using a local variable would require to occupy the maximum 
space during compile time.

But I got your point. Actually we are allocating the space for the worst 
case in the thread stack then, which is no real benefit compared to the 
local variable ... in most cases.

My general advice is, to avoid large local variables at all, unless you 
can exactly calculate the required stack space under any runtime 
condition. Because of nested function calls, this is possible for very 
simple program flows only. We usually try to estimate (and simulate) the 
worst case conditions and then add some extra safety.

Heap space can be re-used dynamically. As a result, more memory is 
available for the majority of real world applications than with pure 
static allocation. Allocating space from stack memory is quasi-static, 
because the space is allocated for the whole lifetime of the thread.

In this special case, we are talking about library functions, which 
should not make optimistic assumptions about available stack space.

After considering this, I changed my mind. I now think that alloca() is 
no good idea for library functions. But I reserve the freedom to change 
my mind within a few hours again. ;-)

Harald






More information about the En-Nut-Discussion mailing list