prev up next   top/contents search

comp.lang.c FAQ list · Question 7.32

Q: What is alloca and why is its use discouraged?

A: alloca allocates memory which is automatically freed when the function which called alloca returns. That is, memory allocated with alloca is local to a particular function's ``stack frame'' or context.

alloca cannot be written portably, [footnote] and is difficult to implement on machines without a conventional stack. Its use is problematical (and the obvious implementation on a stack-based machine fails) when its return value is passed directly to another function, as in fgets(alloca(100), 100, stdin). [footnote]

For these reasons, alloca is not Standard and cannot be used in programs which must be widely portable, no matter how useful it might be. Now that C99 supports variable-length arrays (VLA's), they can be used to more cleanly accomplish most of the tasks which alloca used to be put to.

See also question 7.22.

Additional links: an article by Gordon L. Burditt describing difficulties in implementing and using alloca

References: Rationale Sec. 4.10.3

prev up next   contents search
about this FAQ list   about eskimo   search   feedback   copyright

Hosted by Eskimo North