Possible Duplicate:
In which cases is alloca() useful?
I recently happened to see the use of alloca() function. A google search told me that it’s used to allocate space on the stack. I’m not able to grab the application for it ? Also, are there any pitfalls in using this ?
The function
allocawas never part of any C standard. It has typically been supplied by vendors as an extension to achieve something like the variable-length arrays (“VLA”s) in C99:In both cases, the memory is automatically managed and released at the end of the function.
The use of
allocais mentally burdensome, since it doesn’t quite fit the C object model. For example, even if you allocate the memory in a nested scope, it remains live until the end of the enclosing function, not just the block. VLAs have much better semantics, and can be queried with a dynamicsizeof, whereas there is no equivalent mechanism foralloca-allocated memory.