alloca()
allocates memory from Stack rather than heap which is case in malloc()
. So, when I return from the routine the memory is freed. So, actually this solves my problem of freeing up of dynamically allocated memory. Freeing of memory allocated through malloc()
is a major headache and if somehow missed leads to all sorts memory problems.
Why is the use of alloca()
discouraged in spite of the above features?
Processes only have a limited amount of stack space available - far less than the amount of memory available to
malloc()
.By using
alloca()
you dramatically increase your chances of getting a Stack Overflow error (if you're lucky, or an inexplicable crash if you're not).